2022-02-04 03:04:56 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
import { getUser } from './utils'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { Comment } from '../../common/comment'
|
|
|
|
import { User } from '../../common/user'
|
2022-06-18 21:31:39 +00:00
|
|
|
import {
|
|
|
|
cleanUsername,
|
|
|
|
cleanDisplayName,
|
|
|
|
} from '../../common/util/clean-username'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { Answer } from '../../common/answer'
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
export const changeUserInfo = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
|
|
|
.https.onCall(
|
|
|
|
async (
|
|
|
|
data: {
|
|
|
|
username?: string
|
|
|
|
name?: string
|
|
|
|
avatarUrl?: string
|
|
|
|
},
|
|
|
|
context
|
|
|
|
) => {
|
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
|
|
|
const user = await getUser(userId)
|
|
|
|
if (!user) return { status: 'error', message: 'User not found' }
|
|
|
|
|
|
|
|
const { username, name, avatarUrl } = data
|
|
|
|
|
|
|
|
return await changeUser(user, { username, name, avatarUrl })
|
|
|
|
.then(() => {
|
|
|
|
console.log('succesfully changed', user.username, 'to', data)
|
|
|
|
return { status: 'success' }
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log('Error', e.message)
|
|
|
|
return { status: 'error', message: e.message }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
export const changeUser = async (
|
|
|
|
user: User,
|
|
|
|
update: {
|
|
|
|
username?: string
|
|
|
|
name?: string
|
|
|
|
avatarUrl?: string
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
|
|
|
if (update.username) {
|
|
|
|
update.username = cleanUsername(update.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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 21:31:39 +00:00
|
|
|
if (update.name) {
|
|
|
|
update.name = cleanDisplayName(update.name)
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:04:56 +00:00
|
|
|
const userRef = firestore.collection('users').doc(user.id)
|
|
|
|
const userUpdate: Partial<User> = removeUndefinedProps(update)
|
|
|
|
|
|
|
|
const contractsRef = firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.where('creatorId', '==', user.id)
|
|
|
|
|
|
|
|
const contracts = await transaction.get(contractsRef)
|
|
|
|
|
|
|
|
const contractUpdate: Partial<Contract> = removeUndefinedProps({
|
|
|
|
creatorName: update.name,
|
|
|
|
creatorUsername: update.username,
|
|
|
|
creatorAvatarUrl: update.avatarUrl,
|
|
|
|
})
|
|
|
|
|
|
|
|
const commentSnap = await transaction.get(
|
|
|
|
firestore
|
|
|
|
.collectionGroup('comments')
|
|
|
|
.where('userUsername', '==', user.username)
|
|
|
|
)
|
|
|
|
|
|
|
|
const commentUpdate: Partial<Comment> = removeUndefinedProps({
|
|
|
|
userName: update.name,
|
|
|
|
userUsername: update.username,
|
|
|
|
userAvatarUrl: update.avatarUrl,
|
|
|
|
})
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const answerSnap = await transaction.get(
|
|
|
|
firestore
|
|
|
|
.collectionGroup('answers')
|
|
|
|
.where('username', '==', user.username)
|
|
|
|
)
|
|
|
|
const answerUpdate: Partial<Answer> = removeUndefinedProps(update)
|
|
|
|
|
2022-02-04 03:04:56 +00:00
|
|
|
await transaction.update(userRef, userUpdate)
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
commentSnap.docs.map((d) => transaction.update(d.ref, commentUpdate))
|
|
|
|
)
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
await Promise.all(
|
|
|
|
answerSnap.docs.map((d) => transaction.update(d.ref, answerUpdate))
|
|
|
|
)
|
|
|
|
|
2022-02-04 03:04:56 +00:00
|
|
|
await contracts.docs.map((d) => transaction.update(d.ref, contractUpdate))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|