adding data and keeping users index updated

This commit is contained in:
Arjun Patel
2022-02-01 12:52:34 -08:00
parent f20eaea04e
commit c86d8566ac
6 changed files with 189 additions and 5 deletions
@@ -0,0 +1,9 @@
{
"env": {
"algolia": {
"ALGOLIA_APP_ID": "1RAMCAU1FW",
"ALGOLIA_API_KEY": "c44a7dda47bfb69527b061b7b5994d56",
"ALGOLIA_USERS_INDEX_NAME": "test_USERS"
}
}
}
+7
View File
@@ -0,0 +1,7 @@
{
"algolia": {
"ALGOLIA_APP_ID": "1RAMCAU1FW",
"ALGOLIA_API_KEY": "c44a7dda47bfb69527b061b7b5994d56",
"ALGOLIA_USERS_INDEX_NAME": "test_USERS"
}
}
+7 -4
View File
@@ -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",
+4 -1
View File
@@ -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;
@@ -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 });
});