diff --git a/db/versions/11043-blackRoebelini/00-logLocker.sql b/db/versions/11043-blackRoebelini/00-logLocker.sql new file mode 100644 index 000000000..ba615420b --- /dev/null +++ b/db/versions/11043-blackRoebelini/00-logLocker.sql @@ -0,0 +1,2 @@ +ALTER TABLE vn.workerLog + MODIFY COLUMN changedModel enum('Worker','Calendar','WorkerTimeControlMail','Business','WorkerDms','WorkerTimeControl', 'Locker') NOT NULL DEFAULT 'Worker'; diff --git a/modules/worker/back/models/locker.js b/modules/worker/back/models/locker.js new file mode 100644 index 000000000..4475c2cd1 --- /dev/null +++ b/modules/worker/back/models/locker.js @@ -0,0 +1,21 @@ +module.exports = Self => { + Self.observe('before save', async ctx => { + const models = Self.app.models; + const changes = ctx.data || ctx.instance; + const instance = ctx.currentInstance; + + const workerFk = changes?.workerFk || instance?.workerFk; + if (workerFk) { + const locker = await models.Locker.findOne({ + where: {workerFk} + }, ctx.options); + + if (locker) { + await Self.rawSql( + 'UPDATE locker SET workerFk = NULL where workerFk = ?', + [workerFk], + ctx.options); + } + } + }); +}; diff --git a/modules/worker/back/models/worker.json b/modules/worker/back/models/worker.json index 76ad08d09..adfe2c58e 100644 --- a/modules/worker/back/models/worker.json +++ b/modules/worker/back/models/worker.json @@ -96,7 +96,7 @@ "foreignKey": "workerFk" }, "locker": { - "type": "hasOne", + "type": "hasMany", "model": "Locker", "foreignKey": "workerFk" }