changeUserInfo: use transaction
This commit is contained in:
parent
986abc14b7
commit
343f98e1c8
|
@ -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,47 +46,56 @@ export const changeUser = async (
|
||||||
avatarUrl?: string
|
avatarUrl?: string
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
if (update.username) {
|
return await firestore.runTransaction(async (transaction) => {
|
||||||
update.username = cleanUsername(update.username)
|
if (update.username) {
|
||||||
if (!update.username) {
|
update.username = cleanUsername(update.username)
|
||||||
throw new Error('Invalid username')
|
if (!update.username) {
|
||||||
|
throw new Error('Invalid username')
|
||||||
|
}
|
||||||
|
|
||||||
|
const sameNameUser = await transaction.get(
|
||||||
|
firestore.collection('users').where('username', '==', update.username)
|
||||||
|
)
|
||||||
|
if (!sameNameUser.empty) {
|
||||||
|
throw new Error('Username already exists')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sameNameUser = await getUserByUsername(update.username)
|
const userRef = firestore.collection('users').doc(user.id)
|
||||||
if (sameNameUser) {
|
const userUpdate: Partial<User> = removeUndefinedProps(update)
|
||||||
throw new Error('Username already exists')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const userRef = firestore.collection('users').doc(user.id)
|
const contractsRef = firestore
|
||||||
|
.collection('contracts')
|
||||||
|
.where('creatorId', '==', user.id)
|
||||||
|
|
||||||
const userUpdate: Partial<User> = removeUndefinedProps(update)
|
const contracts = await transaction.get(contractsRef)
|
||||||
await userRef.update(userUpdate)
|
|
||||||
|
|
||||||
const contractSnap = await firestore
|
const contractUpdate: Partial<Contract> = removeUndefinedProps({
|
||||||
.collection('contracts')
|
creatorName: update.name,
|
||||||
.where('creatorId', '==', user.id)
|
creatorUsername: update.username,
|
||||||
.get()
|
creatorAvatarUrl: update.avatarUrl,
|
||||||
|
})
|
||||||
|
|
||||||
const contractUpdate: Partial<Contract> = removeUndefinedProps({
|
const commentSnap = await transaction.get(
|
||||||
creatorName: update.name,
|
firestore
|
||||||
creatorUsername: update.username,
|
.collectionGroup('comments')
|
||||||
creatorAvatarUrl: update.avatarUrl,
|
.where('userUsername', '==', user.username)
|
||||||
|
)
|
||||||
|
|
||||||
|
const commentUpdate: Partial<Comment> = removeUndefinedProps({
|
||||||
|
userName: update.name,
|
||||||
|
userUsername: update.username,
|
||||||
|
userAvatarUrl: update.avatarUrl,
|
||||||
|
})
|
||||||
|
|
||||||
|
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))
|
||||||
})
|
})
|
||||||
await Promise.all(contractSnap.docs.map((d) => d.ref.update(contractUpdate)))
|
|
||||||
|
|
||||||
const commentSnap = await firestore
|
|
||||||
.collectionGroup('comments')
|
|
||||||
.where('userUsername', '==', user.username)
|
|
||||||
.get()
|
|
||||||
|
|
||||||
const commentUpdate: Partial<Comment> = removeUndefinedProps({
|
|
||||||
userName: update.name,
|
|
||||||
userUsername: update.username,
|
|
||||||
userAvatarUrl: update.avatarUrl,
|
|
||||||
})
|
|
||||||
|
|
||||||
await Promise.all(commentSnap.docs.map((d) => d.ref.update(commentUpdate)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user