394c60f2cf
Revert "Revert "[web] Convert models and self-referential test files to ESM "" GitOrigin-RevId: f64000ae31d298b075a8722dfc51f294c71bc021
38 lines
884 B
JavaScript
38 lines
884 B
JavaScript
import mongodb from 'mongodb-legacy'
|
|
import { Institution as InstitutionModel } from '../../../../app/src/models/Institution.mjs'
|
|
|
|
const { ObjectId } = mongodb
|
|
|
|
let count = parseInt(Math.random() * 999999)
|
|
|
|
class Institution {
|
|
constructor(options = {}) {
|
|
this.v1Id = options.v1Id || count
|
|
this.managerIds = []
|
|
|
|
count += 1
|
|
}
|
|
|
|
async ensureExists() {
|
|
const filter = { v1Id: this.v1Id }
|
|
const options = { upsert: true, new: true, setDefaultsOnInsert: true }
|
|
const institution = await InstitutionModel.findOneAndUpdate(
|
|
filter,
|
|
{},
|
|
options
|
|
)
|
|
this._id = institution._id
|
|
}
|
|
|
|
setManagerIds(managerIds, callback) {
|
|
return InstitutionModel.findOneAndUpdate(
|
|
{ _id: new ObjectId(this._id) },
|
|
{ managerIds }
|
|
)
|
|
.then((...args) => callback(null, ...args))
|
|
.catch(callback)
|
|
}
|
|
}
|
|
|
|
export default Institution
|