-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: migrating functions to typescript
- Loading branch information
Showing
25 changed files
with
3,534 additions
and
3,459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"functions": { | ||
"runtime": "nodejs14", | ||
"runtime": "nodejs20", | ||
"source": "./functions" | ||
}, | ||
"hosting": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
index.html | ||
dist |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
Oops, something went wrong.