Institution.js 850 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const { ObjectId } = require('mongodb-legacy')
  2. const InstitutionModel =
  3. require('../../../../app/src/models/Institution').Institution
  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. ensureExists(callback) {
  12. const filter = { v1Id: this.v1Id }
  13. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  14. InstitutionModel.findOneAndUpdate(
  15. filter,
  16. {},
  17. options,
  18. (error, institution) => {
  19. this._id = institution._id
  20. callback(error)
  21. }
  22. )
  23. }
  24. setManagerIds(managerIds, callback) {
  25. return InstitutionModel.findOneAndUpdate(
  26. { _id: new ObjectId(this._id) },
  27. { managerIds },
  28. callback
  29. )
  30. }
  31. }
  32. module.exports = Institution