Skip to content

Commit

Permalink
wip: migrating functions to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Sep 10, 2024
1 parent 771bc03 commit 1533e09
Show file tree
Hide file tree
Showing 25 changed files with 3,534 additions and 3,459 deletions.
2 changes: 1 addition & 1 deletion firebase.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"functions": {
"runtime": "nodejs14",
"runtime": "nodejs20",
"source": "./functions"
},
"hosting": {
Expand Down
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
index.html
dist
File renamed without changes.
25 changes: 15 additions & 10 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"type": "module",
"main": "dist/main.js",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"build": "npx tsc -p ./tsconfig.json",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"deploy:dev": "firebase deploy -P develop --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"@google-cloud/logging": "^7.3.0",
"firebase": "^7.14.0",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.0",
"firebase-tools": "^8.0.3",
"isbot": "^2.5.7",
"ramda": "^0.27.0"
"@google-cloud/logging": "^11.2.0",
"@types/ramda": "^0.30.2",
"firebase": "^10.13.1",
"firebase-admin": "^12.4.0",
"firebase-functions": "^5.1.1",
"firebase-tools": "^13.16.0",
"isbot": "^5.1.17",
"ramda": "^0.30.1",
"typescript": "^5.6.2"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-promise": "^4.2.1",
"firebase-functions-test": "^0.2.0"
"eslint": "^9.10.0",
"eslint-plugin-promise": "^7.1.0",
"firebase-functions-test": "^3.3.0"
},
"private": true
}
38 changes: 0 additions & 38 deletions functions/src/add_project_file_on_storage_upload.js

This file was deleted.

41 changes: 41 additions & 0 deletions functions/src/add_project_file_on_storage_upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { firestore } from "firebase-admin";
import { storage } from "firebase-functions";
import { makeLogger } from "./logger.js";

const log = makeLogger("addProjectFileOnStorageUpload");

const newTimestamp = firestore.FieldValue.serverTimestamp;

// Add a project file entry in Firestore when a binary file is uploaded
export const addProjectFileOnStorageUploadCallback = storage
.object()
.onFinalize(async (obj) => {
const metadata = obj.metadata;

if (metadata) {
const { userUid, projectUid, docUid, filename } = metadata;

if (userUid && projectUid && docUid && filename) {
const collection = `/projects/${userUid}/${projectUid}/files`;
log(
`addProjectFileOnStorageUploadCallback: Adding project file entry in Firestore: ${collection} ${docUid} ${filename}`
);

await firestore()
.collection("projects")
.doc(projectUid)
.collection("files")
.doc(docUid)
.set({
name: filename,
type: "bin",
userUid,
value: "",
created: newTimestamp(),
lastModified: newTimestamp()
});
}
}

return true;
});
119 changes: 0 additions & 119 deletions functions/src/delete_user.js

This file was deleted.

130 changes: 130 additions & 0 deletions functions/src/delete_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import { makeLogger } from "./logger.js";

const log = makeLogger("deleteUser");

const deleteUserDocument = async (
user: admin.auth.UserRecord
): Promise<void> => {
log(`deleteUserDocument: Deleting user document for: ${user.displayName}`);
try {
await admin.firestore().collection("users").doc(user.uid).delete();
} catch (error) {
log(
"error: " + JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
};

const deleteProfileDocument = async (
user: admin.auth.UserRecord
): Promise<void> => {
log(`deleteProfileDocument: Deleting profile for: ${user.displayName}`);
try {
await admin.firestore().collection("profiles").doc(user.uid).delete();
} catch (error) {
log(
"error: " + JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
};

const deleteUsernameDocument = async (
user: admin.auth.UserRecord
): Promise<void> => {
log(
`deleteUsernameDocument: Deleting the username of: ${user.displayName}`
);
try {
const querySnapshot = await admin
.firestore()
.collection("usernames")
.where("userUid", "==", user.uid)
.get();
querySnapshot.forEach(async (doc) => {
await admin.firestore().doc(doc.ref.path).delete();
});
} catch (error) {
log(
"error: " + JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
};

const deleteUserProjects = async (
user: admin.auth.UserRecord
): Promise<void> => {
log(`deleteProjects: Deleting projects created by: ${user.displayName}`);
const batch = admin.firestore().batch();

try {
const allProjectsRef = await admin
.firestore()
.collection("projects")
.where("userUid", "==", user.uid)
.get();

await Promise.all(
allProjectsRef.docs.map(async (doc) => {
const projectRef = admin.firestore().doc(doc.ref.path);
const projectSubcolls = await projectRef.listCollections();

await Promise.all(
projectSubcolls.map(async (subcoll) => {
const subcollDocs = await subcoll.get();
subcollDocs.forEach((subcollDoc) => {
batch.delete(subcollDoc.ref);
});
})
);

const projectLastModifiedRef = admin
.firestore()
.collection("projectLastModified")
.doc(projectRef.id);
batch.delete(projectLastModifiedRef);
batch.delete(projectRef);
})
);

await batch.commit();
} catch (error) {
log(
"error: " + JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
};

const deleteProjectsCount = async (
user: admin.auth.UserRecord
): Promise<void> => {
log(
`deleteProjectsCount: Deleting projectsCount for: ${user.displayName} under ${user.uid}`
);
try {
await admin
.firestore()
.collection("projectsCount")
.doc(user.uid)
.delete();
} catch (error) {
log(
"error: " + JSON.stringify(error, Object.getOwnPropertyNames(error))
);
}
};

export const deleteUserCallback = functions.auth
.user()
.onDelete(async (user) => {
log(
`deleteUserCallback: Removing user: ${user.displayName}, with uid ${user.uid}`
);
await deleteUserProjects(user);
await deleteProfileDocument(user);
await deleteUserDocument(user);
await deleteUsernameDocument(user);
await deleteProjectsCount(user);
return true;
});
Loading

0 comments on commit 1533e09

Please sign in to comment.