diff --git a/packages/serverless/functions/.runtimeconfig.json b/packages/serverless/functions/.runtimeconfig.json new file mode 100644 index 0000000..561102a --- /dev/null +++ b/packages/serverless/functions/.runtimeconfig.json @@ -0,0 +1,9 @@ +{ + "env": { + "algolia": { + "ALGOLIA_APP_ID": "1RAMCAU1FW", + "ALGOLIA_API_KEY": "c44a7dda47bfb69527b061b7b5994d56", + "ALGOLIA_USERS_INDEX_NAME": "test_USERS" + } + } +} diff --git a/packages/serverless/functions/env.json b/packages/serverless/functions/env.json new file mode 100644 index 0000000..657c8f0 --- /dev/null +++ b/packages/serverless/functions/env.json @@ -0,0 +1,7 @@ +{ + "algolia": { + "ALGOLIA_APP_ID": "1RAMCAU1FW", + "ALGOLIA_API_KEY": "c44a7dda47bfb69527b061b7b5994d56", + "ALGOLIA_USERS_INDEX_NAME": "test_USERS" + } +} diff --git a/packages/serverless/functions/package.json b/packages/serverless/functions/package.json index 59db235..80f7b51 100644 --- a/packages/serverless/functions/package.json +++ b/packages/serverless/functions/package.json @@ -4,21 +4,24 @@ "scripts": { "lint": "eslint --ext .js,.ts .", "build": "npm run lint && tsc", - "serve": "npm run build && firebase emulators:start --only functions", + "serve": "npm run build && firebase functions:config:get > .runtimeconfig.json && firebase emulators:start --only functions", "shell": "npm run build && firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log", "hotReload": "tsc --watch", - "copyModels": "cp -r ../../web/models ./models" + "copyModels": "cp -r ../../web/models ./models", + "env": "test -f env.json && firebase functions:config:unset env && firebase functions:config:set env=\"$(cat env.json)\" || echo \"Please add the file env.json before deploy.\"" }, "main": "lib/index.js", "dependencies": { + "@nirvana/common": "*", "@sendgrid/mail": "^7.6.0", + "@types/algoliasearch": "^4.0.0", "agora-access-token": "^2.0.4", + "algoliasearch": "^4.12.1", "firebase-admin": "^9.8.0", - "firebase-functions": "^3.14.1", - "@nirvana/common": "*" + "firebase-functions": "^3.14.1" }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^3.9.1", diff --git a/packages/serverless/functions/src/index.ts b/packages/serverless/functions/src/index.ts index 60ec134..4d3ea94 100644 --- a/packages/serverless/functions/src/index.ts +++ b/packages/serverless/functions/src/index.ts @@ -9,10 +9,13 @@ admin.initializeApp(); export const helloWorld = functions.https.onRequest((request, response) => { functions.logger.info("Hello no!", { structuredData: true }); response.send("Hello from woahhhhhh!"); -}) +}); import { agoraToken } from "./agora"; exports.agoraToken = agoraToken; import { inviteUserToTeam } from "./notifications"; exports.inviteUserToTeam = inviteUserToTeam; + +import { manageUserIndex } from "./search"; +exports.manageUserIndex = manageUserIndex; diff --git a/packages/serverless/functions/src/search.ts b/packages/serverless/functions/src/search.ts new file mode 100644 index 0000000..dfe2aed --- /dev/null +++ b/packages/serverless/functions/src/search.ts @@ -0,0 +1,26 @@ +import * as functions from "firebase-functions"; +const config = functions.config().env; + +import algoliasearch from "algoliasearch"; + +const client = algoliasearch(config.ALGOLIA_APP_ID, config.ALGOLIA_API_KEY); + +const USERS_INDEX_NAME: string = config.ALGOLIA_USERS_INDEX_NAME; +const usersIndex = client.initIndex(USERS_INDEX_NAME); + +export const manageUserIndex = functions.firestore + .document("/users/{userId}") + .onWrite((change, context) => { + // Get an object with the current document value. + // If the document does not exist, it has been deleted. + const objectId = change.before.id; + const document = change.after.exists ? change.after.data() : null; + + // delete the index if the user is deleted + if (!document) { + return usersIndex.deleteObject(objectId); + } + + // otherwise, just update the index with new user information + return usersIndex.saveObject({ objectId, ...document }); + }); diff --git a/yarn.lock b/yarn.lock index 97ab4dd..517a3ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,110 @@ # yarn lockfile v1 +"@algolia/cache-browser-local-storage@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/cache-browser-local-storage/-/cache-browser-local-storage-4.12.1.tgz#23f4f219963b96918d0524acd09d4d646541d888" + integrity sha512-ERFFOnC9740xAkuO0iZTQqm2AzU7Dpz/s+g7o48GlZgx5p9GgNcsuK5eS0GoW/tAK+fnKlizCtlFHNuIWuvfsg== + dependencies: + "@algolia/cache-common" "4.12.1" + +"@algolia/cache-common@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/cache-common/-/cache-common-4.12.1.tgz#d3f1676ca9c404adce0f78d68f6381bedb44cd9c" + integrity sha512-UugTER3V40jT+e19Dmph5PKMeliYKxycNPwrPNADin0RcWNfT2QksK9Ff2N2W7UKraqMOzoeDb4LAJtxcK1a8Q== + +"@algolia/cache-in-memory@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/cache-in-memory/-/cache-in-memory-4.12.1.tgz#0ef6aac2f8feab5b46fc130beb682bbd21b55244" + integrity sha512-U6iaunaxK1lHsAf02UWF58foKFEcrVLsHwN56UkCtwn32nlP9rz52WOcHsgk6TJrL8NDcO5swMjtOQ5XHESFLw== + dependencies: + "@algolia/cache-common" "4.12.1" + +"@algolia/client-account@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/client-account/-/client-account-4.12.1.tgz#e838c9283db2fab32a425dd13c77da321d48fd8b" + integrity sha512-jGo4ConJNoMdTCR2zouO0jO/JcJmzOK6crFxMMLvdnB1JhmMbuIKluOTJVlBWeivnmcsqb7r0v7qTCPW5PAyxQ== + dependencies: + "@algolia/client-common" "4.12.1" + "@algolia/client-search" "4.12.1" + "@algolia/transporter" "4.12.1" + +"@algolia/client-analytics@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/client-analytics/-/client-analytics-4.12.1.tgz#2976d658655a1590cf84cfb596aa75a204f6dec4" + integrity sha512-h1It7KXzIthlhuhfBk7LteYq72tym9maQDUsyRW0Gft8b6ZQahnRak9gcCvKwhcJ1vJoP7T7JrNYGiYSicTD9g== + dependencies: + "@algolia/client-common" "4.12.1" + "@algolia/client-search" "4.12.1" + "@algolia/requester-common" "4.12.1" + "@algolia/transporter" "4.12.1" + +"@algolia/client-common@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/client-common/-/client-common-4.12.1.tgz#104ccefe96bda3ff926bc70c31ff6d17c41b6107" + integrity sha512-obnJ8eSbv+h94Grk83DTGQ3bqhViSWureV6oK1s21/KMGWbb3DkduHm+lcwFrMFkjSUSzosLBHV9EQUIBvueTw== + dependencies: + "@algolia/requester-common" "4.12.1" + "@algolia/transporter" "4.12.1" + +"@algolia/client-personalization@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/client-personalization/-/client-personalization-4.12.1.tgz#f63d1890f95de850e1c8e41c1d57adda521d9e7f" + integrity sha512-sMSnjjPjRgByGHYygV+5L/E8a6RgU7l2GbpJukSzJ9GRY37tHmBHuvahv8JjdCGJ2p7QDYLnQy5bN5Z02qjc7Q== + dependencies: + "@algolia/client-common" "4.12.1" + "@algolia/requester-common" "4.12.1" + "@algolia/transporter" "4.12.1" + +"@algolia/client-search@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/client-search/-/client-search-4.12.1.tgz#fcd7a974be5d39d5c336d7f2e89577ffa66aefdd" + integrity sha512-MwwKKprfY6X2nJ5Ki/ccXM2GDEePvVjZnnoOB2io3dLKW4fTqeSRlC5DRXeFD7UM0vOPPHr4ItV2aj19APKNVQ== + dependencies: + "@algolia/client-common" "4.12.1" + "@algolia/requester-common" "4.12.1" + "@algolia/transporter" "4.12.1" + +"@algolia/logger-common@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/logger-common/-/logger-common-4.12.1.tgz#d6501b4d9d242956257ba8e10f6b4bbf6863baa4" + integrity sha512-fCgrzlXGATNqdFTxwx0GsyPXK+Uqrx1SZ3iuY2VGPPqdt1a20clAG2n2OcLHJpvaa6vMFPlJyWvbqAgzxdxBlQ== + +"@algolia/logger-console@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/logger-console/-/logger-console-4.12.1.tgz#841edd39dd5c5530a69fc66084bfee3254dd0807" + integrity sha512-0owaEnq/davngQMYqxLA4KrhWHiXujQ1CU3FFnyUcMyBR7rGHI48zSOUpqnsAXrMBdSH6rH5BDkSUUFwsh8RkQ== + dependencies: + "@algolia/logger-common" "4.12.1" + +"@algolia/requester-browser-xhr@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.12.1.tgz#2d0c18ee188d7cae0e4a930e5e89989e3c4a816b" + integrity sha512-OaMxDyG0TZG0oqz1lQh9e3woantAG1bLnuwq3fmypsrQxra4IQZiyn1x+kEb69D2TcXApI5gOgrD4oWhtEVMtw== + dependencies: + "@algolia/requester-common" "4.12.1" + +"@algolia/requester-common@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/requester-common/-/requester-common-4.12.1.tgz#95bb6539da7199da3e205341cea8f27267f7af29" + integrity sha512-XWIrWQNJ1vIrSuL/bUk3ZwNMNxl+aWz6dNboRW6+lGTcMIwc3NBFE90ogbZKhNrFRff8zI4qCF15tjW+Fyhpow== + +"@algolia/requester-node-http@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/requester-node-http/-/requester-node-http-4.12.1.tgz#c9df97ff1daa7e58c5c2b1f28cf7163005edccb0" + integrity sha512-awBtwaD+s0hxkA1aehYn8F0t9wqGoBVWgY4JPHBmp1ChO3pK7RKnnvnv7QQa9vTlllX29oPt/BBVgMo1Z3n1Qg== + dependencies: + "@algolia/requester-common" "4.12.1" + +"@algolia/transporter@4.12.1": + version "4.12.1" + resolved "https://registry.yarnpkg.com/@algolia/transporter/-/transporter-4.12.1.tgz#61b9829916c474f42e2d4a6eada0d6c138379945" + integrity sha512-BGeNgdEHc6dXIk2g8kdlOoQ6fQ6OIaKQcplEj7HPoi+XZUeAvRi3Pff3QWd7YmybWkjzd9AnTzieTASDWhL+sQ== + dependencies: + "@algolia/cache-common" "4.12.1" + "@algolia/logger-common" "4.12.1" + "@algolia/requester-common" "4.12.1" + "@ant-design/colors@^6.0.0": version "6.0.0" resolved "https://registry.yarnpkg.com/@ant-design/colors/-/colors-6.0.0.tgz#9b9366257cffcc47db42b9d0203bb592c13c0298" @@ -934,6 +1038,13 @@ resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf" integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A== +"@types/algoliasearch@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@types/algoliasearch/-/algoliasearch-4.0.0.tgz#129501dff14b70024439751beb3ec820417f566a" + integrity sha512-XMGE0h/dRV7qR6lR/zgGL01esc39pLHNl4m7gKZldrgoov/IaFJGfmmEJGxktlH63esWEtAiXzbjLfvEQXN15Q== + dependencies: + algoliasearch "*" + "@types/body-parser@*": version "1.19.2" resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.2.tgz#aea2059e28b7658639081347ac4fab3de166e6f0" @@ -1074,6 +1185,11 @@ "@types/mime" "^1" "@types/node" "*" +"@types/uuid@^8.3.4": + version "8.3.4" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc" + integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw== + "@typescript-eslint/eslint-plugin@^3.9.1": version "3.10.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-3.10.1.tgz#7e061338a1383f59edc204c605899f93dc2e2c8f" @@ -1373,6 +1489,26 @@ ajv@^8.0.1: require-from-string "^2.0.2" uri-js "^4.2.2" +algoliasearch@*, algoliasearch@^4.12.1: + version "4.12.1" + resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-4.12.1.tgz#574a2c5424c4b6681c026928fb810be2d2ec3924" + integrity sha512-c0dM1g3zZBJrkzE5GA/Nu1y3fFxx3LCzxKzcmp2dgGS8P4CjszB/l3lsSh2MSrrK1Hn/KV4BlbBMXtYgG1Bfrw== + dependencies: + "@algolia/cache-browser-local-storage" "4.12.1" + "@algolia/cache-common" "4.12.1" + "@algolia/cache-in-memory" "4.12.1" + "@algolia/client-account" "4.12.1" + "@algolia/client-analytics" "4.12.1" + "@algolia/client-common" "4.12.1" + "@algolia/client-personalization" "4.12.1" + "@algolia/client-search" "4.12.1" + "@algolia/logger-common" "4.12.1" + "@algolia/logger-console" "4.12.1" + "@algolia/requester-browser-xhr" "4.12.1" + "@algolia/requester-common" "4.12.1" + "@algolia/requester-node-http" "4.12.1" + "@algolia/transporter" "4.12.1" + anser@1.4.9: version "1.4.9" resolved "https://registry.yarnpkg.com/anser/-/anser-1.4.9.tgz#1f85423a5dcf8da4631a341665ff675b96845760"