-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6420 from NMDSdevopsServiceAdm/featureBranch/qual…
…sCertificate Feature branch/quals certificate
- Loading branch information
Showing
68 changed files
with
5,257 additions
and
1,455 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
backend/migrations/20241008122413-createTableForQualificationsCertificates.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,71 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
return queryInterface.createTable( | ||
'QualificationCertificates', | ||
{ | ||
ID: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
UID: { | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
WorkerQualificationsFK: { | ||
type: Sequelize.DataTypes.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: { | ||
tableName: 'WorkerQualifications', | ||
schema: 'cqc', | ||
}, | ||
key: 'ID', | ||
}, | ||
}, | ||
WorkerFK: { | ||
type: Sequelize.DataTypes.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: { | ||
tableName: 'Worker', | ||
schema: 'cqc', | ||
}, | ||
key: 'ID', | ||
}, | ||
}, | ||
FileName: { | ||
type: Sequelize.DataTypes.TEXT, | ||
allowNull: false, | ||
}, | ||
UploadDate: { | ||
type: Sequelize.DataTypes.DATE, | ||
allowNull: true, | ||
}, | ||
Key: { | ||
type: Sequelize.DataTypes.TEXT, | ||
allowNull: false, | ||
}, | ||
}, | ||
{ schema: 'cqc' }, | ||
); | ||
}, | ||
|
||
async down(queryInterface) { | ||
/** | ||
* Add reverting commands here. | ||
* | ||
* Example: | ||
* await queryInterface.dropTable('users'); | ||
*/ | ||
return queryInterface.dropTable({ | ||
tableName: 'QualificationCertificates', | ||
schema: 'cqc', | ||
}); | ||
}, | ||
}; |
84 changes: 84 additions & 0 deletions
84
backend/server/models/classes/helpers/bulkUploadQualificationHelper.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,84 @@ | ||
const { compact } = require('lodash'); | ||
|
||
const models = require('../../index'); | ||
const WorkerCertificateService = require('../../../routes/establishments/workerCertificate/workerCertificateService'); | ||
|
||
class BulkUploadQualificationHelper { | ||
constructor({ workerId, workerUid, establishmentId, savedBy, externalTransaction }) { | ||
this.workerId = workerId; | ||
this.workerUid = workerUid; | ||
this.establishmentId = establishmentId; | ||
this.savedBy = savedBy; | ||
this.bulkUploaded = true; | ||
this.externalTransaction = externalTransaction; | ||
this.qualificationCertificateService = WorkerCertificateService.initialiseQualifications(); | ||
} | ||
|
||
async processQualificationsEntities(qualificationsEntities) { | ||
const promisesToReturn = []; | ||
|
||
const allQualificationRecords = await models.workerQualifications.findAll({ | ||
where: { | ||
workerFk: this.workerId, | ||
}, | ||
}); | ||
|
||
for (const bulkUploadEntity of qualificationsEntities) { | ||
const currentQualificationId = bulkUploadEntity?._qualification?.id; | ||
const existingQualification = allQualificationRecords.find( | ||
(record) => record.qualificationFk === currentQualificationId, | ||
); | ||
|
||
if (existingQualification) { | ||
promisesToReturn.push(this.updateQualification(existingQualification, bulkUploadEntity)); | ||
} else { | ||
promisesToReturn.push(this.createNewQualification(bulkUploadEntity)); | ||
} | ||
} | ||
|
||
const bulkUploadQualificationFks = compact(qualificationsEntities.map((entity) => entity?._qualification?.id)); | ||
const qualificationsToDelete = allQualificationRecords.filter( | ||
(qualification) => !bulkUploadQualificationFks.includes(qualification.qualificationFk), | ||
); | ||
for (const qualification of qualificationsToDelete) { | ||
promisesToReturn.push(this.deleteQualification(qualification)); | ||
} | ||
|
||
return promisesToReturn; | ||
} | ||
|
||
createNewQualification(entityFromBulkUpload) { | ||
entityFromBulkUpload.workerId = this.workerId; | ||
entityFromBulkUpload.workerUid = this.workerUid; | ||
entityFromBulkUpload.establishmentId = this.establishmentId; | ||
|
||
return entityFromBulkUpload.save(this.savedBy, this.bulkUploaded, 0, this.externalTransaction); | ||
} | ||
|
||
updateQualification(existingRecord, entityFromBulkUpload) { | ||
const fieldsToUpdate = { | ||
source: 'Bulk', | ||
updatedBy: this.savedBy.toLowerCase(), | ||
notes: entityFromBulkUpload.notes ?? existingRecord.notes, | ||
year: entityFromBulkUpload.year ?? existingRecord.year, | ||
}; | ||
|
||
existingRecord.set(fieldsToUpdate); | ||
|
||
return existingRecord.save({ transaction: this.externalTransaction }); | ||
} | ||
|
||
async deleteQualification(existingRecord) { | ||
const certificatesFound = await existingRecord.getQualificationCertificates(); | ||
if (certificatesFound?.length) { | ||
await this.qualificationCertificateService.deleteCertificatesWithTransaction( | ||
certificatesFound, | ||
this.externalTransaction, | ||
); | ||
} | ||
|
||
return existingRecord.destroy({ transaction: this.externalTransaction }); | ||
} | ||
} | ||
|
||
module.exports = BulkUploadQualificationHelper; |
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.