|
| 1 | +const ReviewObjectModel = require('../model/reviewobject') |
| 2 | +const BaseRepository = require('./baseRepository') |
| 3 | +const BaseOrgRepository = require('./baseOrgRepository') |
| 4 | +const uuid = require('uuid') |
| 5 | + |
| 6 | +class ReviewObjectRepository extends BaseRepository { |
| 7 | + async findOneByOrgShortName (orgShortName, options = {}) { |
| 8 | + const baseOrgRepository = new BaseOrgRepository() |
| 9 | + const org = await baseOrgRepository.findOneByShortName(orgShortName) |
| 10 | + if (!org) { |
| 11 | + throw new Error(`No organization found with short name ${orgShortName}`) |
| 12 | + } |
| 13 | + const reviewObject = await ReviewObjectModel.find({ target_object_uuid: org.UUID }, null, options) |
| 14 | + |
| 15 | + return reviewObject || null |
| 16 | + } |
| 17 | + |
| 18 | + async findOneByUUID (UUID, options = {}) { |
| 19 | + const reviewObject = await ReviewObjectModel.findOne({ uuid: UUID }, null, options) |
| 20 | + return reviewObject || null |
| 21 | + } |
| 22 | + |
| 23 | + async getAllReviewObjects (options = {}) { |
| 24 | + const reviewObjects = await ReviewObjectModel.find({}, null, options) |
| 25 | + return reviewObjects || [] |
| 26 | + } |
| 27 | + |
| 28 | + async deleteReviewObjectByUUID (UUID, options = {}) { |
| 29 | + const result = await ReviewObjectModel.deleteOne({ uuid: UUID }, options) |
| 30 | + return result.deletedCount |
| 31 | + } |
| 32 | + |
| 33 | + async getOrgReviewObjectByOrgShortname (orgShortName, options = {}) { |
| 34 | + const baseOrgRepository = new BaseOrgRepository() |
| 35 | + const org = await baseOrgRepository.findOneByShortName(orgShortName) |
| 36 | + if (!org) { |
| 37 | + throw new Error(`No organization found with short name ${orgShortName}`) |
| 38 | + } |
| 39 | + const reviewObject = await ReviewObjectModel.findOne({ target_object_uuid: org.UUID }, null, options) |
| 40 | + |
| 41 | + return reviewObject || null |
| 42 | + } |
| 43 | + |
| 44 | + async getOrgReviewObjectByOrgUUID (orgUUID, options = {}) { |
| 45 | + const baseOrgRepository = new BaseOrgRepository() |
| 46 | + const org = await baseOrgRepository.findOneByUUID(orgUUID) |
| 47 | + if (!org) { |
| 48 | + throw new Error(`No organization found with UUID ${orgUUID}`) |
| 49 | + } |
| 50 | + const reviewObject = await ReviewObjectModel.findOne({ target_object_uuid: org.UUID }, null, options) |
| 51 | + |
| 52 | + return reviewObject || null |
| 53 | + } |
| 54 | + |
| 55 | + async createReviewOrgObject (body, options = {}) { |
| 56 | + console.log('Creating review object for organization:', body.target_object_uuid) |
| 57 | + body.uuid = uuid.v4() |
| 58 | + const reviewObject = new ReviewObjectModel(body) |
| 59 | + const result = await reviewObject.save(options) |
| 60 | + return result.toObject() |
| 61 | + } |
| 62 | + |
| 63 | + async updateReviewOrgObject (body, UUID, options = {}) { |
| 64 | + console.log('Updating review object with UUID:', UUID) |
| 65 | + const reviewObject = await this.findOneByUUID(UUID, options) |
| 66 | + |
| 67 | + // For each item waiting for approval, for testing we are going to just do shortname |
| 68 | + reviewObject.new_review_data.short_name = body.new_review_data.short_name || reviewObject.new_review_data.short_name |
| 69 | + |
| 70 | + const result = await reviewObject.save(options) |
| 71 | + return result.toObject() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = ReviewObjectRepository |
0 commit comments