Institution.mjs 884 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import mongodb from 'mongodb-legacy'
  2. import { Institution as InstitutionModel } from '../../../../app/src/models/Institution.mjs'
  3. const { ObjectId } = mongodb
  4. let count = parseInt(Math.random() * 999999)
  5. class Institution {
  6. constructor(options = {}) {
  7. this.v1Id = options.v1Id || count
  8. this.managerIds = []
  9. count += 1
  10. }
  11. async ensureExists() {
  12. const filter = { v1Id: this.v1Id }
  13. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  14. const institution = await InstitutionModel.findOneAndUpdate(
  15. filter,
  16. {},
  17. options
  18. )
  19. this._id = institution._id
  20. }
  21. setManagerIds(managerIds, callback) {
  22. return InstitutionModel.findOneAndUpdate(
  23. { _id: new ObjectId(this._id) },
  24. { managerIds }
  25. )
  26. .then((...args) => callback(null, ...args))
  27. .catch(callback)
  28. }
  29. }
  30. export default Institution