changeUserInfo: use transaction

This commit is contained in:
mantikoros 2022-02-03 19:39:59 -06:00
parent 986abc14b7
commit 343f98e1c8

View File

@ -1,7 +1,7 @@
import * as functions from 'firebase-functions' import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { getUser, getUserByUsername, removeUndefinedProps } from './utils' import { getUser, removeUndefinedProps } from './utils'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { Comment } from '../../common/comment' import { Comment } from '../../common/comment'
import { User } from '../../common/user' import { User } from '../../common/user'
@ -46,39 +46,41 @@ export const changeUser = async (
avatarUrl?: string avatarUrl?: string
} }
) => { ) => {
return await firestore.runTransaction(async (transaction) => {
if (update.username) { if (update.username) {
update.username = cleanUsername(update.username) update.username = cleanUsername(update.username)
if (!update.username) { if (!update.username) {
throw new Error('Invalid username') throw new Error('Invalid username')
} }
const sameNameUser = await getUserByUsername(update.username) const sameNameUser = await transaction.get(
if (sameNameUser) { firestore.collection('users').where('username', '==', update.username)
)
if (!sameNameUser.empty) {
throw new Error('Username already exists') throw new Error('Username already exists')
} }
} }
const userRef = firestore.collection('users').doc(user.id) const userRef = firestore.collection('users').doc(user.id)
const userUpdate: Partial<User> = removeUndefinedProps(update) const userUpdate: Partial<User> = removeUndefinedProps(update)
await userRef.update(userUpdate)
const contractSnap = await firestore const contractsRef = firestore
.collection('contracts') .collection('contracts')
.where('creatorId', '==', user.id) .where('creatorId', '==', user.id)
.get()
const contracts = await transaction.get(contractsRef)
const contractUpdate: Partial<Contract> = removeUndefinedProps({ const contractUpdate: Partial<Contract> = removeUndefinedProps({
creatorName: update.name, creatorName: update.name,
creatorUsername: update.username, creatorUsername: update.username,
creatorAvatarUrl: update.avatarUrl, creatorAvatarUrl: update.avatarUrl,
}) })
await Promise.all(contractSnap.docs.map((d) => d.ref.update(contractUpdate)))
const commentSnap = await firestore const commentSnap = await transaction.get(
firestore
.collectionGroup('comments') .collectionGroup('comments')
.where('userUsername', '==', user.username) .where('userUsername', '==', user.username)
.get() )
const commentUpdate: Partial<Comment> = removeUndefinedProps({ const commentUpdate: Partial<Comment> = removeUndefinedProps({
userName: update.name, userName: update.name,
@ -86,7 +88,14 @@ export const changeUser = async (
userAvatarUrl: update.avatarUrl, userAvatarUrl: update.avatarUrl,
}) })
await Promise.all(commentSnap.docs.map((d) => d.ref.update(commentUpdate))) await transaction.update(userRef, userUpdate)
await Promise.all(
commentSnap.docs.map((d) => transaction.update(d.ref, commentUpdate))
)
await contracts.docs.map((d) => transaction.update(d.ref, contractUpdate))
})
} }
const firestore = admin.firestore() const firestore = admin.firestore()