forked from bcgov/common-hosted-form-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: FORMS-1042 new document templates backend (bcgov#1317)
- Loading branch information
1 parent
871c7b3
commit f65b032
Showing
15 changed files
with
969 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
app/src/db/migrations/20240403192833_044-document-templates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const uuid = require('uuid'); | ||
|
||
const stamps = require('../stamps'); | ||
const { Permissions, Roles } = require('../../forms/common/constants'); | ||
|
||
const CREATED_BY = 'migration-044'; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.up = function (knex) { | ||
return Promise.resolve().then(() => | ||
knex.schema | ||
.createTable('document_template', (table) => { | ||
table.uuid('id').primary(); | ||
table.uuid('formId').references('id').inTable('form').notNullable().index(); | ||
table.boolean('active').defaultTo(true); | ||
table.string('filename', 1024).notNullable(); | ||
table.binary('template').notNullable(); | ||
stamps(knex, table); | ||
}) | ||
|
||
.then(() => | ||
knex.schema.alterTable('form', (table) => { | ||
table.boolean('enableDocumentTemplates').defaultTo(false).comment('Allow document templates to be stored'); | ||
}) | ||
) | ||
|
||
.then(() => { | ||
const permission = { | ||
createdBy: CREATED_BY, | ||
code: Permissions.DOCUMENT_TEMPLATE_CREATE, | ||
display: 'Document Template Create', | ||
description: 'Can create document templates for a form', | ||
active: true, | ||
}; | ||
return knex('permission').insert(permission); | ||
}) | ||
.then(() => { | ||
const permission = { | ||
createdBy: CREATED_BY, | ||
code: Permissions.DOCUMENT_TEMPLATE_DELETE, | ||
display: 'Document Template Delete', | ||
description: 'Can delete document templates for a form', | ||
active: true, | ||
}; | ||
return knex('permission').insert(permission); | ||
}) | ||
.then(() => { | ||
const permission = { | ||
createdBy: CREATED_BY, | ||
code: Permissions.DOCUMENT_TEMPLATE_READ, | ||
display: 'Document Template Read', | ||
description: 'Can view document templates for a form', | ||
active: true, | ||
}; | ||
return knex('permission').insert(permission); | ||
}) | ||
|
||
.then(() => { | ||
const rolePermission = { | ||
id: uuid.v4(), | ||
createdBy: CREATED_BY, | ||
role: Roles.OWNER, | ||
permission: Permissions.DOCUMENT_TEMPLATE_CREATE, | ||
}; | ||
return knex('role_permission').insert(rolePermission); | ||
}) | ||
.then(() => { | ||
const rolePermission = { | ||
id: uuid.v4(), | ||
createdBy: CREATED_BY, | ||
role: Roles.OWNER, | ||
permission: Permissions.DOCUMENT_TEMPLATE_DELETE, | ||
}; | ||
return knex('role_permission').insert(rolePermission); | ||
}) | ||
.then(() => { | ||
const rolePermission = { | ||
id: uuid.v4(), | ||
createdBy: CREATED_BY, | ||
role: Roles.OWNER, | ||
permission: Permissions.DOCUMENT_TEMPLATE_READ, | ||
}; | ||
return knex('role_permission').insert(rolePermission); | ||
}) | ||
); | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.down = function (knex) { | ||
return Promise.resolve() | ||
.then(() => | ||
knex('role_permission') | ||
.where({ | ||
createdBy: CREATED_BY, | ||
}) | ||
.del() | ||
) | ||
|
||
.then(() => | ||
knex('permission') | ||
.where({ | ||
createdBy: CREATED_BY, | ||
}) | ||
.del() | ||
) | ||
|
||
.then(() => | ||
knex.schema.alterTable('form', (table) => { | ||
table.dropColumn('enableDocumentTemplates'); | ||
}) | ||
) | ||
|
||
.then(() => knex.schema.dropTableIfExists('document_template')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { Model } = require('objection'); | ||
const { Timestamps } = require('../mixins'); | ||
const { Regex } = require('../../constants'); | ||
const stamps = require('../jsonSchema').stamps; | ||
|
||
class DocumentTemplate extends Timestamps(Model) { | ||
static get tableName() { | ||
return 'document_template'; | ||
} | ||
|
||
static get modifiers() { | ||
return { | ||
filterActive(query, value) { | ||
if (value) { | ||
query.where('active', value); | ||
} | ||
}, | ||
filterFormId(query, value) { | ||
if (value) { | ||
query.where('formId', value); | ||
} | ||
}, | ||
filterId(query, value) { | ||
if (value) { | ||
query.where('id', value); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['filename', 'formId', 'template'], | ||
properties: { | ||
id: { type: 'string', pattern: Regex.UUID }, | ||
formId: { type: 'string', pattern: Regex.UUID }, | ||
active: { type: 'boolean' }, | ||
filename: { type: 'string' }, | ||
template: { type: 'string' }, | ||
...stamps, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = DocumentTemplate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.