Skip to content

Commit

Permalink
Revert onUserWritten changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljohanneskraft committed Nov 10, 2024
1 parent ecb0497 commit 6df4c03
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
31 changes: 21 additions & 10 deletions functions/src/functions/onUserWritten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,41 @@

import { type User, userConverter } from '@stanfordbdhg/engagehf-models'
import { logger } from 'firebase-functions'
import { onDocumentCreated } from 'firebase-functions/v2/firestore'
import { onDocumentWritten } from 'firebase-functions/v2/firestore'
import { DatabaseConverter } from '../services/database/databaseConverter.js'
import { type Document } from '../services/database/databaseService.js'
import { getServiceFactory } from '../services/factory/getServiceFactory.js'

export const onUserWritten = onDocumentCreated(
export const onUserWritten = onDocumentWritten(
'users/{userId}',
async (event) => {
if (event.data === undefined) return
const factory = getServiceFactory()
const userService = factory.user()
try {
const converter = new DatabaseConverter(userConverter.value)
const userDoc: Document<User> = {
id: event.params.userId,
path: event.document,
content: converter.fromFirestore(event.data),
lastUpdate: new Date(event.time),
if (
event.data?.before.exists !== true &&
event.data?.after.exists === true
) {
const converter = new DatabaseConverter(userConverter.value)
const userDoc: Document<User> = {
id: event.params.userId,
path: event.document,
content: converter.fromFirestore(event.data.after),
lastUpdate: new Date(event.time),
}
await userService.finishUserEnrollment(userDoc)
}
await userService.finishUserEnrollment(userDoc)
} catch (error) {
logger.error(
`Error finishing enrollment for user with id '${event.params.userId}' on change of user: ${String(error)}`,
)
}
try {
await userService.updateClaims(event.params.userId)
} catch (error) {
logger.error(
`Error processing claims update for userId '${event.params.userId}' on change of user: ${String(error)}`,
)
}
},
)
15 changes: 15 additions & 0 deletions functions/src/services/user/databaseUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ export class DatabaseUserService implements UserService {
})
}

async updateClaims(userId: string): Promise<void> {
const user = await this.getUser(userId)
if (user === undefined) {
logger.error(
`DatabaseUserService.updateClaims(${userId}): User not found.`,
)
throw new https.HttpsError('not-found', 'User not found.')
}
logger.info(`DatabaseUserService.updateClaims(${userId}): User found.`)
await this.auth.setCustomUserClaims(userId, user.content.claims)
logger.info(
`DatabaseUserService.updateClaims(${userId}): User claims updated.`,
)
}

// Invitations

async createInvitation(content: Invitation): Promise<{ id: string }> {
Expand Down
4 changes: 4 additions & 0 deletions functions/src/services/user/userService.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class MockUserService implements UserService {
return
}

async updateClaims(userId: String): Promise<void> {

Check failure on line 47 in functions/src/services/user/userService.mock.ts

View workflow job for this annotation

GitHub Actions / Lint

Don't use `String` as a type. Use string instead
return
}

// Methods - Invitations

async createInvitation(content: Invitation): Promise<{ id: string }> {
Expand Down
1 change: 1 addition & 0 deletions functions/src/services/user/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface UserService {

getAuth(userId: string): Promise<UserAuth>
updateAuth(userId: string, auth: UserAuth): Promise<void>
updateClaims(userId: String): Promise<void>

Check failure on line 27 in functions/src/services/user/userService.ts

View workflow job for this annotation

GitHub Actions / Lint

Don't use `String` as a type. Use string instead

// Invitations

Expand Down

0 comments on commit 6df4c03

Please sign in to comment.