|
| 1 | +import functions from 'firebase-functions'; |
| 2 | +import { getFirestore, Timestamp, FieldValue } from 'firebase-admin/firestore'; |
| 3 | + |
| 4 | +import approveUserData, { |
| 5 | + approveUserResult, |
| 6 | +} from '@common/functions/approveUser'; |
| 7 | + |
| 8 | +import User from '@common/types/User'; |
| 9 | +import { getAuth } from 'firebase-admin/auth'; |
| 10 | + |
| 11 | +const approveUser = functions |
| 12 | + .region('us-west2') |
| 13 | + .https.onCall(async (data: approveUserData, context) => { |
| 14 | + const firestore = getFirestore(); |
| 15 | + |
| 16 | + // App Check Verification |
| 17 | + if (!context.app && process.env.NODE_ENV === 'production') { |
| 18 | + throw new functions.https.HttpsError( |
| 19 | + 'unauthenticated', |
| 20 | + 'The function must be called from an App Check verified app.' |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + // Auth Verification |
| 25 | + if (!context.auth) { |
| 26 | + throw new functions.https.HttpsError( |
| 27 | + 'unauthenticated', |
| 28 | + 'The function must be called while authenticated.' |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + if ( |
| 33 | + !context.auth.token?.permissions?.MANAGE_USERS?.includes( |
| 34 | + data.libraryID |
| 35 | + ) && |
| 36 | + !context.auth.token?.librariesOwned?.includes(data.libraryID) |
| 37 | + ) { |
| 38 | + throw new functions.https.HttpsError( |
| 39 | + 'permission-denied', |
| 40 | + "The user calling the function must have the 'MANAGE_USERS' permission." |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + console.log(data, typeof data.expiration); |
| 45 | + |
| 46 | + // Type Verification |
| 47 | + if ( |
| 48 | + typeof data.firstName !== 'string' || |
| 49 | + typeof data.lastName !== 'string' || |
| 50 | + typeof data.email !== 'string' || |
| 51 | + typeof data.uid !== 'string' || |
| 52 | + typeof data.libraryID !== 'string' || |
| 53 | + typeof data.expiration !== 'number' |
| 54 | + ) { |
| 55 | + throw new functions.https.HttpsError( |
| 56 | + 'invalid-argument', |
| 57 | + 'The function must be called with the appropriate arguments.' |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + const libraryDoc = await firestore |
| 62 | + .collection('libraries') |
| 63 | + .doc(data.libraryID) |
| 64 | + .get(); |
| 65 | + |
| 66 | + if (!libraryDoc.exists) { |
| 67 | + throw new functions.https.HttpsError( |
| 68 | + 'not-found', |
| 69 | + 'The specified library could not be found.' |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + const user = await getAuth().getUser(data.uid); |
| 74 | + |
| 75 | + if (!user) { |
| 76 | + throw new functions.https.HttpsError( |
| 77 | + 'not-found', |
| 78 | + 'The specified user could not be found.' |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + // Create a batched update |
| 83 | + const batch = firestore.batch(); |
| 84 | + |
| 85 | + const userDoc: User = { |
| 86 | + active: true, |
| 87 | + activeCheckouts: [], |
| 88 | + checkoutGroup: 'default', |
| 89 | + firstName: data.firstName, |
| 90 | + approvedAt: FieldValue.serverTimestamp() as Timestamp, |
| 91 | + approvedBy: context.auth.uid, |
| 92 | + lastName: data.lastName, |
| 93 | + email: data.email, |
| 94 | + phoneNumber: null, |
| 95 | + identifiers: [], |
| 96 | + expiration: Timestamp.fromDate(new Date(data.expiration)), |
| 97 | + uid: data.uid, |
| 98 | + updatedBy: context.auth.uid, |
| 99 | + updatedAt: FieldValue.serverTimestamp() as Timestamp, |
| 100 | + }; |
| 101 | + |
| 102 | + const userRef = firestore |
| 103 | + .collection(`libraries/${data.libraryID}/users`) |
| 104 | + .doc(data.uid); |
| 105 | + |
| 106 | + const joinRequestRef = firestore |
| 107 | + .collection(`libraries/${data.libraryID}/joinRequests`) |
| 108 | + .doc(data.uid); |
| 109 | + |
| 110 | + batch.set(userRef, userDoc); |
| 111 | + batch.update(joinRequestRef, { approved: true }); |
| 112 | + |
| 113 | + await batch |
| 114 | + .commit() |
| 115 | + .then(async () => { |
| 116 | + const userClaims = user.customClaims; |
| 117 | + |
| 118 | + const newLibrariesJoined = []; |
| 119 | + if (userClaims && Array.isArray(userClaims?.librariesJoined)) { |
| 120 | + newLibrariesJoined.push(userClaims.librariesJoined); |
| 121 | + newLibrariesJoined.push(data.libraryID); |
| 122 | + } else { |
| 123 | + newLibrariesJoined.push(data.libraryID); |
| 124 | + } |
| 125 | + |
| 126 | + getAuth() |
| 127 | + .setCustomUserClaims(data.uid, { |
| 128 | + ...userClaims, |
| 129 | + librariesJoined: newLibrariesJoined, |
| 130 | + }) |
| 131 | + .catch(console.error); |
| 132 | + }) |
| 133 | + .catch((err) => { |
| 134 | + functions.logger.error(context, data, err); |
| 135 | + throw new functions.https.HttpsError( |
| 136 | + 'internal', |
| 137 | + 'There was an error approving this user.' |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + const result: approveUserResult = { |
| 142 | + userID: data.uid, |
| 143 | + }; |
| 144 | + |
| 145 | + return result; |
| 146 | + }); |
| 147 | + |
| 148 | +export default approveUser; |
0 commit comments