Skip to content

Commit

Permalink
Update catalog record model
Browse files Browse the repository at this point in the history
  • Loading branch information
tusbar committed Oct 24, 2018
1 parent 73a0960 commit 3b6b6c2
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions lib/models/CatalogRecord.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
'use strict'

/* eslint no-multi-spaces: 0, key-spacing: 0 */
const mongoose = require('mongoose')
const {Schema} = require('mongoose')
const {pick} = require('lodash')

const ObjectId = Schema.Types.ObjectId
const {ObjectId} = mongoose.Schema.Types

const collectionName = 'catalog_records'

/* Schema */
const schema = new Schema({
const schema = new mongoose.Schema({

/* Identification */
catalog: {type: ObjectId, ref: 'Service', required: true},
recordId: {type: String, required: true},
catalog: {
type: ObjectId,
ref: 'Service',
required: true
},
recordId: {
type: String,
required: true
},

/* Attributes */
recordHash: {type: String, required: true},
revisionDate: {type: Date},
recordHash: {
type: String,
required: true
},
revisionDate: {
type: Date
},

/* Dates */
createdAt: {type: Date},
touchedAt: {type: Date},
updatedAt: {type: Date}
createdAt: {
type: Date
},
touchedAt: {
type: Date
},
updatedAt: {
type: Date
}

})

Expand All @@ -33,42 +47,50 @@ schema.index({catalog: 1, recordId: 1}, {unique: true})
/* Statics */
schema.statics = {

touchExisting(catalogRecord) {
async touchExisting(catalogRecord) {
const now = new Date()
const query = pick(catalogRecord, 'catalog', 'recordId', 'recordHash')
const changes = {$set: {touchedAt: now}}

return this.update(query, changes).exec()
.then(rawResponse => rawResponse.nModified === 1)
const response = await this.update(query, {
$set: {
touchedAt: now
}
})

return response.nModified === 1
},

doUpsert(catalogRecord) {
async doUpsert(catalogRecord) {
const now = new Date()
const query = pick(catalogRecord, 'catalog', 'recordId')
const changes = {

const response = await this.update(query, {
$setOnInsert: {createdAt: now},
$set: {
recordHash: catalogRecord.recordHash,
revisionDate: catalogRecord.revisionDate,
updatedAt: now,
touchedAt: now
}
}
})

return this.update(query, changes, {upsert: true}).exec()
.then(rawResponse => rawResponse.upserted ? 'created' : 'updated')
return response.upserted ? 'created' : 'updated'
},

upsert(catalogRecord) {
return this
.touchExisting(catalogRecord)
.then(touched => touched ? 'touched' : this.doUpsert(catalogRecord))
.then(async upsertStatus => {
if (upsertStatus === 'created') {
await mongoose.model('ConsolidatedRecord').triggerUpdated(catalogRecord.recordId, 'added to catalog')
}
return upsertStatus
})
async upsert(catalogRecord) {
const touched = await this.touchExisting(catalogRecord)

if (touched) {
return 'touched'
}

const status = await this.doUpsert(catalogRecord)

if (status === 'created') {
await mongoose.model('ConsolidatedRecord').triggerUpdated(catalogRecord.recordId, 'added to catalog')
}

return status
}

}
Expand Down

0 comments on commit 3b6b6c2

Please sign in to comment.