refs #5063 refactored hook
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alexandre Riera 2023-02-20 10:39:12 +01:00
parent 2a8cb17297
commit c7f22a10ba
3 changed files with 25 additions and 40 deletions

View File

@ -40,7 +40,6 @@ module.exports = Self => {
const TempContainer = models.TempContainer;
const fileOptions = {};
const args = ctx.args;
const user = ctx.req.accessToken.userId;
let srcFile;
try {
@ -60,7 +59,7 @@ module.exports = Self => {
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
srcFile = path.join(file.client.root, file.container, file.name);
await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id, user);
await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id);
} catch (e) {
if (fs.existsSync(srcFile))
await fs.unlink(srcFile);

View File

@ -46,7 +46,7 @@ module.exports = Self => {
}).bitmap;
}
Self.registerImage = async(collectionName, srcFilePath, fileName, entityId, user) => {
Self.registerImage = async(collectionName, srcFilePath, fileName, entityId) => {
const models = Self.app.models;
const tx = await Self.beginTransaction({});
const myOptions = {transaction: tx};
@ -140,7 +140,7 @@ module.exports = Self => {
const item = await model.findById(entityId, null, myOptions);
if (item) {
collection.model == 'item' ? fileName = fileName + '-' + user : fileName;
if (collection.model == 'Item') fileName += '#';
await item.updateAttribute(
collection.property,
fileName,

View File

@ -21,52 +21,38 @@ module.exports = Self => {
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});
Self.observe('before save', async function(ctx) {
let instance = ctx.currentInstance;
let oldInstance;
let newInstance;
ctx.hookState.oldInstance === undefined ? oldInstance = false : oldInstance = ctx.hookState.oldInstance;
ctx.hookState.newInstance === undefined ? newInstance = false : newInstance = ctx.hookState.newInstance;
if (oldInstance.image && newInstance.image) {
let image = newInstance.image.split('-')[0];
let user = newInstance.image.split('-')[1];
if (ctx.isNewInstance) await Self.availableId(ctx);
else {
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
ctx.hookState.newInstance.image = image;
const hasChanges = orgData && changes;
const image = changes.image || orgData.image;
const imageChanged = hasChanges && orgData.image != image;
let query = `UPDATE vn.item SET doPhoto=0 WHERE id = ${instance.id}`;
await Self.rawSql(query);
let logQuery =
'INSERT INTO vn.itemLog(originFk, userFk, action, creationDate, description, oldInstance, newInstance) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?)';
let logParams = [
instance.id,
user,
'update',
new Date(),
'Image',
JSON.stringify(oldInstance),
JSON.stringify(newInstance)
];
await Self.rawSql(logQuery, logParams);
if (imageChanged) {
try {
changes.image = changes.image.slice(0, -1);
if (orgData.isPhotoRequested == true) changes.isPhotoRequested = false;
} catch (e) {
throw new UserError(e);
}
}
}
await Self.availableId(ctx);
});
Self.availableId = async function(ctx) {
if (ctx.isNewInstance) {
try {
let query = `SELECT i1.id + 1 as id FROM vn.item i1
try {
let query = `SELECT i1.id + 1 as id FROM vn.item i1
LEFT JOIN vn.item i2 ON i1.id + 1 = i2.id
WHERE i2.id IS NULL ORDER BY i1.id LIMIT 1`;
let newId = await Self.rawSql(query);
let newId = await Self.rawSql(query);
ctx.instance.id = newId[0].id;
return ctx.instance.id;
} catch (e) {
throw new UserError(e);
}
ctx.instance.id = newId[0].id;
return ctx.instance.id;
} catch (e) {
throw new UserError(e);
}
};
};