Merge branch 'dev' into 6264-renewToken
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
commit
10bfafdd3c
|
@ -22,8 +22,8 @@ module.exports = Self => {
|
|||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
let tx;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
|
|
@ -3,7 +3,7 @@ CREATE OR REPLACE DEFINER=`root`@`localhost`
|
|||
VIEW `vn`.`ticketState`
|
||||
AS SELECT `tt`.`created` AS `updated`,
|
||||
`tt`.`stateFk` AS `stateFk`,
|
||||
`tt`.`userFk` AS `userFk`,
|
||||
`tt`.`userFk` AS `workerFk`,
|
||||
`tls`.`ticketFk` AS `ticketFk`,
|
||||
`s`.`id` AS `state`,
|
||||
`s`.`order` AS `productionOrder`,
|
||||
|
@ -40,7 +40,7 @@ SELECT
|
|||
`ts`.`state` AS `state`,
|
||||
`ts`.`productionOrder` AS `productionOrder`,
|
||||
`ts`.`alertLevel` AS `alertLevel`,
|
||||
`ts`.`userFk` AS `userFk`,
|
||||
`ts`.`worker` AS `worker`,
|
||||
`ts`.`code` AS `code`,
|
||||
`ts`.`updated` AS `updated`,
|
||||
`ts`.`isPicked` AS `isPicked`
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('removeFile', {
|
||||
description: 'Removes a claim document',
|
||||
|
@ -19,8 +21,8 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
let tx;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
@ -31,19 +33,18 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
try {
|
||||
const models = Self.app.models;
|
||||
const targetClaimDms = await models.ClaimDms.findById(id, null, myOptions);
|
||||
const targetDms = await models.Dms.findById(targetClaimDms.dmsFk, null, myOptions);
|
||||
const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions);
|
||||
const claimDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
await models.Dms.removeFile(ctx, targetClaimDms.dmsFk, myOptions);
|
||||
await targetClaimDms.destroy(myOptions);
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions);
|
||||
|
||||
await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
|
||||
if (!targetDms || ! claimDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const claimDmsDestroyed = await claimDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return targetDms;
|
||||
return claimDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('uploadFile', {
|
||||
|
@ -57,96 +54,33 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.uploadFile = async(ctx, id, options) => {
|
||||
const tx = await Self.beginTransaction({});
|
||||
const {Dms, ClaimDms} = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction)
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const models = Self.app.models;
|
||||
const promises = [];
|
||||
const TempContainer = models.TempContainer;
|
||||
const ClaimContainer = models.ClaimContainer;
|
||||
const fileOptions = {};
|
||||
const args = ctx.args;
|
||||
|
||||
let srcFile;
|
||||
try {
|
||||
const hasWriteRole = await models.DmsType.hasWriteRole(ctx, args.dmsTypeId, myOptions);
|
||||
if (!hasWriteRole)
|
||||
throw new UserError(`You don't have enough privileges`);
|
||||
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
|
||||
|
||||
// Upload file to temporary path
|
||||
const tempContainer = await TempContainer.container('dms');
|
||||
const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
|
||||
const files = Object.values(uploaded.files).map(file => {
|
||||
return file[0];
|
||||
});
|
||||
|
||||
const addedDms = [];
|
||||
for (const uploadedFile of files) {
|
||||
const newDms = await createDms(ctx, uploadedFile, myOptions);
|
||||
const pathHash = ClaimContainer.getHash(newDms.id);
|
||||
|
||||
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
||||
srcFile = path.join(file.client.root, file.container, file.name);
|
||||
|
||||
const claimContainer = await ClaimContainer.container(pathHash);
|
||||
const dstFile = path.join(claimContainer.client.root, pathHash, newDms.file);
|
||||
|
||||
await fs.move(srcFile, dstFile, {
|
||||
overwrite: true
|
||||
});
|
||||
|
||||
addedDms.push(newDms);
|
||||
}
|
||||
|
||||
addedDms.forEach(dms => {
|
||||
const newClaimDms = models.ClaimDms.create({
|
||||
claimFk: id,
|
||||
dmsFk: dms.id
|
||||
}, myOptions);
|
||||
|
||||
promises.push(newClaimDms);
|
||||
});
|
||||
const resolvedPromises = await Promise.all(promises);
|
||||
const promises = uploadedFiles.map(dms => ClaimDms.create({
|
||||
claimFk: id,
|
||||
dmsFk: dms.id
|
||||
}, myOptions));
|
||||
await Promise.all(promises);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return resolvedPromises;
|
||||
return uploadedFiles;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
|
||||
if (fs.existsSync(srcFile))
|
||||
await fs.unlink(srcFile);
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
async function createDms(ctx, file, myOptions) {
|
||||
const models = Self.app.models;
|
||||
const myUserId = ctx.req.accessToken.userId;
|
||||
const args = ctx.args;
|
||||
|
||||
const newDms = await models.Dms.create({
|
||||
workerFk: myUserId,
|
||||
dmsTypeFk: args.dmsTypeId,
|
||||
companyFk: args.companyId,
|
||||
warehouseFk: args.warehouseId,
|
||||
reference: args.reference,
|
||||
description: args.description,
|
||||
contentType: file.type,
|
||||
hasFile: args.hasFile
|
||||
}, myOptions);
|
||||
|
||||
let fileName = file.name;
|
||||
const extension = models.DmsContainer.getFileExtension(fileName);
|
||||
fileName = `${newDms.id}.${extension}`;
|
||||
|
||||
return newDms.updateAttribute('file', fileName, myOptions);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -19,9 +19,8 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
let tx;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
@ -34,13 +33,16 @@ module.exports = Self => {
|
|||
try {
|
||||
const clientDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
await models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
|
||||
|
||||
const destroyedClient = await clientDms.destroy(myOptions);
|
||||
if (!targetDms || !clientDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const clientDmsDestroyed = await clientDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return destroyedClient;
|
||||
return clientDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
|
|
|
@ -107,17 +107,29 @@ module.exports = Self => {
|
|||
return {or: [
|
||||
{'c.phone': {like: `%${value}%`}},
|
||||
{'c.mobile': {like: `%${value}%`}},
|
||||
{'a.phone': {like: `%${value}%`}},
|
||||
]};
|
||||
case 'zoneFk':
|
||||
param = 'a.postalCode';
|
||||
return {[param]: {inq: postalCode}};
|
||||
return {'a.postalCode': {inq: postalCode}};
|
||||
case 'city':
|
||||
return {or: [
|
||||
{'c.city': {like: `%${value}%`}},
|
||||
{'a.city': {like: `%${value}%`}}
|
||||
]};
|
||||
case 'postcode':
|
||||
return {or: [
|
||||
{'c.postcode': value},
|
||||
{'a.postalCode': value}
|
||||
]};
|
||||
case 'provinceFk':
|
||||
return {or: [
|
||||
{'p.id': value},
|
||||
{'a.provinceFk': value}
|
||||
]};
|
||||
case 'name':
|
||||
case 'salesPersonFk':
|
||||
case 'fi':
|
||||
case 'socialName':
|
||||
case 'city':
|
||||
case 'postcode':
|
||||
case 'provinceFk':
|
||||
case 'email':
|
||||
param = `c.${param}`;
|
||||
return {[param]: {like: `%${value}%`}};
|
||||
|
@ -134,24 +146,29 @@ module.exports = Self => {
|
|||
c.fi,
|
||||
c.socialName,
|
||||
c.phone,
|
||||
a.phone,
|
||||
c.mobile,
|
||||
c.city,
|
||||
a.city,
|
||||
c.postcode,
|
||||
a.postalCode,
|
||||
c.email,
|
||||
c.isActive,
|
||||
c.isFreezed,
|
||||
p.id AS provinceFk,
|
||||
p.id AS provinceClientFk,
|
||||
a.provinceFk AS provinceAddressFk,
|
||||
p.name AS province,
|
||||
u.id AS salesPersonFk,
|
||||
u.name AS salesPerson
|
||||
FROM client c
|
||||
LEFT JOIN account.user u ON u.id = c.salesPersonFk
|
||||
LEFT JOIN province p ON p.id = c.provinceFk
|
||||
JOIN vn.address a ON a.clientFk = c.id
|
||||
JOIN address a ON a.clientFk = c.id
|
||||
`
|
||||
);
|
||||
|
||||
stmt.merge(conn.makeWhere(filter.where));
|
||||
stmt.merge('GROUP BY c.id');
|
||||
stmt.merge(conn.makePagination(filter));
|
||||
|
||||
const clientsIndex = stmts.push(stmt) - 1;
|
||||
|
|
|
@ -55,9 +55,9 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.uploadFile = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
let tx;
|
||||
const {Dms, ClientDms} = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
@ -67,23 +67,20 @@ module.exports = Self => {
|
|||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
|
||||
try {
|
||||
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
||||
uploadedFiles.forEach(dms => {
|
||||
const newClientDms = models.ClientDms.create({
|
||||
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
|
||||
const promises = uploadedFiles.map(dms =>
|
||||
ClientDms.create({
|
||||
clientFk: id,
|
||||
dmsFk: dms.id
|
||||
}, myOptions);
|
||||
}, myOptions)
|
||||
|
||||
promises.push(newClientDms);
|
||||
});
|
||||
const resolvedPromises = await Promise.all(promises);
|
||||
);
|
||||
await Promise.all(promises);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return resolvedPromises;
|
||||
return uploadedFiles;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
|
|
|
@ -19,7 +19,6 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
|
@ -32,18 +31,18 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
try {
|
||||
const targetTicketDms = await models.TicketDms.findById(id, null, myOptions);
|
||||
const targetDms = await models.Dms.findById(targetTicketDms.dmsFk, null, myOptions);
|
||||
const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}, myOptions);
|
||||
const ticketDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
await models.Dms.removeFile(ctx, targetTicketDms.dmsFk, myOptions);
|
||||
await targetTicketDms.destroy(myOptions);
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions);
|
||||
|
||||
await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id, myOptions);
|
||||
if (!targetDms || !ticketDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const ticketDmsDestroyed = await ticketDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return targetDms;
|
||||
return ticketDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
|
|
|
@ -47,7 +47,7 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.uploadFile = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
const {Dms, TicketDms} = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
|
@ -59,22 +59,19 @@ module.exports = Self => {
|
|||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
try {
|
||||
const uploadedFiles = await models.Dms.uploadFile(ctx, myOptions);
|
||||
uploadedFiles.forEach(dms => {
|
||||
const newTicketDms = models.TicketDms.create({
|
||||
ticketFk: id,
|
||||
dmsFk: dms.id
|
||||
}, myOptions);
|
||||
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
|
||||
|
||||
promises.push(newTicketDms);
|
||||
});
|
||||
const resolvedPromises = await Promise.all(promises);
|
||||
const promises = uploadedFiles.map(dms => TicketDms.create({
|
||||
ticketFk: id,
|
||||
dmsFk: dms.id
|
||||
}, myOptions));
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return resolvedPromises;
|
||||
return uploadedFiles;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
|
|
|
@ -34,9 +34,9 @@
|
|||
"foreignKey": "stateFk"
|
||||
},
|
||||
"user": {
|
||||
"type": "belongsTo",
|
||||
"model": "VnUser",
|
||||
"foreignKey": "userFk"
|
||||
}
|
||||
"type": "belongsTo",
|
||||
"model": "VnUser",
|
||||
"foreignKey": "workerFk"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,9 +37,9 @@
|
|||
"foreignKey": "stateFk"
|
||||
},
|
||||
"user": {
|
||||
"type": "belongsTo",
|
||||
"model": "VnUser",
|
||||
"foreignKey": "userFk"
|
||||
}
|
||||
"type": "belongsTo",
|
||||
"model": "VnUser",
|
||||
"foreignKey": "userFk"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,35 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id) => {
|
||||
const models = Self.app.models;
|
||||
const workerDms = await Self.findById(id);
|
||||
Self.removeFile = async(ctx, dmsFk, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
await models.Dms.removeFile(ctx, workerDms.dmsFk);
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
return workerDms.destroy();
|
||||
try {
|
||||
const WorkerDms = await Self.findOne({
|
||||
where: {document: dmsFk}
|
||||
}, myOptions);
|
||||
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || !WorkerDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const workerDmsDestroyed = await WorkerDms.destroy(myOptions);
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return workerDmsDestroyed;
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -47,30 +47,33 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.uploadFile = async(ctx, id) => {
|
||||
const models = Self.app.models;
|
||||
const promises = [];
|
||||
const tx = await Self.beginTransaction({});
|
||||
const {Dms, WorkerDms} = Self.app.models;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
|
||||
uploadedFiles.forEach(dms => {
|
||||
const newWorkerDms = models.WorkerDms.create({
|
||||
const uploadedFiles = await Dms.uploadFile(ctx, myOptions);
|
||||
const promises = uploadedFiles.map(dms =>
|
||||
WorkerDms.create({
|
||||
workerFk: id,
|
||||
dmsFk: dms.id
|
||||
}, options);
|
||||
}, myOptions));
|
||||
await Promise.all(promises);
|
||||
|
||||
promises.push(newWorkerDms);
|
||||
});
|
||||
const resolvedPromises = await Promise.all(promises);
|
||||
if (tx) await tx.commit();
|
||||
|
||||
await tx.commit();
|
||||
|
||||
return resolvedPromises;
|
||||
} catch (err) {
|
||||
await tx.rollback();
|
||||
throw err;
|
||||
return uploadedFiles;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue