43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { Db } from 'mongodb'
|
|
|
|
import { mainDb } from '../../utils/mongo'
|
|
import { cleanId } from '../../utils/common'
|
|
import { usersCollection } from '../../__data__/constants'
|
|
import { Roles } from '../../model/roles'
|
|
|
|
export const registerUser = async ({ username, regtime, role, email, id, ...rest }) => {
|
|
const db: Db = await mainDb
|
|
|
|
const usersCl = db.collection(usersCollection)
|
|
const [registred] = await usersCl.find({ ijlId: id }).toArray()
|
|
|
|
const user = {
|
|
role: Roles.User,
|
|
...(registred || {}),
|
|
ijlId: id,
|
|
ijlUser: {
|
|
username,
|
|
regtime,
|
|
role,
|
|
email,
|
|
...rest
|
|
},
|
|
regtime: regtime || Date.now(),
|
|
}
|
|
|
|
let insertedId
|
|
|
|
if (!registred) {
|
|
const inserted = await usersCl.insertOne(user)
|
|
|
|
insertedId = inserted.insertedId
|
|
} else {
|
|
await usersCl.updateOne({ _id: registred._id }, { $set: user })
|
|
}
|
|
|
|
return cleanId({
|
|
id: insertedId,
|
|
...user
|
|
})
|
|
}
|