authenzify
Version:
server to manage authentication authorization of users and more
66 lines (58 loc) • 1.66 kB
JavaScript
import { verifyExistence } from '../../../util/util.js'
import { generateVerificationId } from '../../../util/record-id-prefixes.js'
const fnList = ['find', 'findOne', 'insertOne', 'updateOne']
export class MongoVerificationsService {
#modelsCollections
constructor(modelsCollections) {
verifyExistence(modelsCollections.Verifications, fnList)
this.#modelsCollections = modelsCollections
}
async delete(id) {
return this.#modelsCollections.Verifications.updateOne(
{
_id: id,
},
{ $set: { isDeleted: true, updatedAt: new Date() } },
)
}
async findByUserId({ userId, type }) {
return this.#modelsCollections.Verifications.findOne({
userId,
type,
})
}
async find(filter) {
return this.#modelsCollections.Verifications.find(filter).toArray()
}
async findOne({ id, type, isDeleted }) {
const verification = await this.#modelsCollections.Verifications.findOne({
_id: id,
type,
isDeleted,
})
return verification
}
async updateMany(updatePart, wherePart) {
return await this.#modelsCollections.Verifications.updateMany(
updatePart,
wherePart,
)
}
async create(verification) {
const id = generateVerificationId()
const now = new Date()
const created = await this.#modelsCollections.Verifications.insertOne({
id,
_id: id,
createdAt: now,
updatedAt: now,
isDeleted: false,
...verification,
})
const verificationUpdated =
await this.#modelsCollections.Verifications.findOne({
id: created?.insertedId,
})
return verificationUpdated
}
}