Skip to content

Add support for isDefault on Challenge Timeline Template Object #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,11 @@ paths:
description: Filter by timeline template id
required: false
type: string
- name: isDefault
in: query
description: Filter by is default flag
required: false
type: boolean
responses:
'200':
description: OK
Expand Down Expand Up @@ -2580,6 +2585,9 @@ definitions:
timelineTemplateId:
type: string
format: UUID
isDefault:
type: boolean
default: false
required:
- typeId
- trackId
Expand Down
4 changes: 4 additions & 0 deletions src/models/ChallengeTimelineTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const schema = new Schema({
timelineTemplateId: {
type: String,
required: true
},
isDefault: {
type: Boolean,
required: false
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,8 @@ async function createChallenge (currentUser, challenge, userToken) {
if (challenge.typeId && challenge.trackId) {
const [challengeTimelineTemplate] = await ChallengeTimelineTemplateService.searchChallengeTimelineTemplates({
typeId: challenge.typeId,
trackId: challenge.trackId
trackId: challenge.trackId,
isDefault: true
})
if (!challengeTimelineTemplate) {
throw new errors.BadRequestError(`The selected trackId ${challenge.trackId} and typeId: ${challenge.typeId} does not have a default timeline template. Please provide a timelineTemplateId`)
Expand Down
36 changes: 32 additions & 4 deletions src/services/ChallengeTimelineTemplateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,36 @@ const constants = require('../../app-constants')
async function searchChallengeTimelineTemplates (criteria) {
const list = await helper.scan('ChallengeTimelineTemplate')
const records = _.filter(list, e => (!criteria.typeId || criteria.typeId === e.typeId) &&
(!criteria.timelineTemplateId || criteria.timelineTemplateId === e.timelineTemplateId))
(!criteria.timelineTemplateId || criteria.timelineTemplateId === e.timelineTemplateId) &&
(!criteria.trackId || criteria.trackId === e.trackId) &&
(!criteria.isDefault || criteria.isDefault === e.isDefault))
return records
}

searchChallengeTimelineTemplates.schema = {
criteria: Joi.object().keys({
typeId: Joi.optionalId(),
trackId: Joi.optionalId(),
timelineTemplateId: Joi.optionalId()
timelineTemplateId: Joi.optionalId(),
isDefault: Joi.boolean()
})
}

/**
* Unset existing default timeline template in order to create a new one
* @param {String} typeId the type ID
* @param {String} trackId the track ID
*/
async function unsetDefaultTimelineTemplate (typeId, trackId) {
const records = await searchChallengeTimelineTemplates({ typeId, trackId, isDefault: true })
if (records.length === 0) {
return
}
for (const record of records) {
await fullyUpdateChallengeTimelineTemplate(record.id, { ...record, isDefault: false })
}
}

/**
* Create challenge type timeline template.
* @param {Object} data the data to create challenge type timeline template
Expand All @@ -46,6 +64,10 @@ async function createChallengeTimelineTemplate (data) {
await helper.getById('ChallengeTrack', data.trackId)
await helper.getById('TimelineTemplate', data.timelineTemplateId)

if (data.isDefault) {
await unsetDefaultTimelineTemplate(data.typeId, data.trackId)
}

const ret = await helper.create('ChallengeTimelineTemplate', _.assign({ id: uuid() }, data))
// post bus event
await helper.postBusEvent(constants.Topics.ChallengeTimelineTemplateCreated, ret)
Expand All @@ -56,7 +78,8 @@ createChallengeTimelineTemplate.schema = {
data: Joi.object().keys({
typeId: Joi.id(),
trackId: Joi.id(),
timelineTemplateId: Joi.id()
timelineTemplateId: Joi.id(),
isDefault: Joi.boolean().default(false).required()
}).required()
}

Expand Down Expand Up @@ -84,7 +107,8 @@ async function fullyUpdateChallengeTimelineTemplate (challengeTimelineTemplateId

if (record.typeId === data.typeId &&
record.trackId === data.trackId &&
record.timelineTemplateId === data.timelineTemplateId) {
record.timelineTemplateId === data.timelineTemplateId &&
record.isDefault === data.isDefault) {
// no change
return record
}
Expand All @@ -99,6 +123,10 @@ async function fullyUpdateChallengeTimelineTemplate (challengeTimelineTemplateId
await helper.getById('ChallengeTrack', data.trackId)
await helper.getById('TimelineTemplate', data.timelineTemplateId)

if (data.isDefault) {
await unsetDefaultTimelineTemplate(data.typeId, data.trackId)
}

const ret = await helper.update(record, data)
// post bus event
await helper.postBusEvent(constants.Topics.ChallengeTimelineTemplateUpdated, ret)
Expand Down