Merge pull request '2669 - Changes to the translate values function' (#577) from 2669-loggable_refactor into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #577 Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
commit
584947c52c
|
@ -134,47 +134,49 @@ module.exports = function(Self) {
|
|||
if (value instanceof Object)
|
||||
continue;
|
||||
|
||||
if (value === undefined || value === null) continue;
|
||||
if (value === undefined) continue;
|
||||
|
||||
for (let relationName in relations) {
|
||||
const relation = relations[relationName];
|
||||
if (relation.keyFrom == key && key != 'id') {
|
||||
const model = relation.modelTo;
|
||||
const modelName = relation.modelTo.modelName;
|
||||
const properties = model && model.definition.properties;
|
||||
const settings = model && model.definition.settings;
|
||||
if (value) {
|
||||
for (let relationName in relations) {
|
||||
const relation = relations[relationName];
|
||||
if (relation.keyFrom == key && key != 'id') {
|
||||
const model = relation.modelTo;
|
||||
const modelName = relation.modelTo.modelName;
|
||||
const properties = model && model.definition.properties;
|
||||
const settings = model && model.definition.settings;
|
||||
|
||||
const recordSet = await appModels[modelName].findById(value, null, options);
|
||||
const recordSet = await appModels[modelName].findById(value, null, options);
|
||||
|
||||
const hasShowField = settings.log && settings.log.showField;
|
||||
let showField = hasShowField && recordSet
|
||||
&& recordSet[settings.log.showField];
|
||||
const hasShowField = settings.log && settings.log.showField;
|
||||
let showField = hasShowField && recordSet
|
||||
&& recordSet[settings.log.showField];
|
||||
|
||||
if (!showField) {
|
||||
const showFieldNames = [
|
||||
'name',
|
||||
'description',
|
||||
'code',
|
||||
'nickname'
|
||||
];
|
||||
for (field of showFieldNames) {
|
||||
const propField = properties && properties[field];
|
||||
const recordField = recordSet && recordSet[field];
|
||||
if (!showField) {
|
||||
const showFieldNames = [
|
||||
'name',
|
||||
'description',
|
||||
'code',
|
||||
'nickname'
|
||||
];
|
||||
for (field of showFieldNames) {
|
||||
const propField = properties && properties[field];
|
||||
const recordField = recordSet && recordSet[field];
|
||||
|
||||
if (propField && recordField) {
|
||||
showField = field;
|
||||
break;
|
||||
if (propField && recordField) {
|
||||
showField = field;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showField && recordSet && recordSet[showField]) {
|
||||
value = recordSet[showField];
|
||||
if (showField && recordSet && recordSet[showField]) {
|
||||
value = recordSet[showField];
|
||||
break;
|
||||
}
|
||||
|
||||
value = recordSet && recordSet.id || value;
|
||||
break;
|
||||
}
|
||||
|
||||
value = recordSet && recordSet.id || value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
result[key] = value;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* @param {Object} instance - The model or context instance
|
||||
* @param {Object} changes - Object containing changes
|
||||
*/
|
||||
exports.translateValues = async(instance, changes) => {
|
||||
exports.translateValues = async(instance, changes, options = {}) => {
|
||||
const models = instance.app.models;
|
||||
function getRelation(instance, property) {
|
||||
const relations = instance.definition.settings.relations;
|
||||
|
@ -38,12 +38,20 @@ exports.translateValues = async(instance, changes) => {
|
|||
|
||||
const properties = Object.assign({}, changes);
|
||||
for (let property in properties) {
|
||||
const firstChar = property.substring(0, 1);
|
||||
const isPrivate = firstChar == '$';
|
||||
if (isPrivate) {
|
||||
delete properties[property];
|
||||
continue;
|
||||
}
|
||||
|
||||
const relation = getRelation(instance, property);
|
||||
const value = properties[property];
|
||||
let finalValue = value;
|
||||
const hasValue = value != null && value != undefined;
|
||||
|
||||
if (relation) {
|
||||
let fieldsToShow = ['alias', 'name', 'code', 'description'];
|
||||
let finalValue = value;
|
||||
if (relation && hasValue) {
|
||||
let fieldsToShow = ['nickname', 'name', 'code', 'description'];
|
||||
const modelName = relation.model;
|
||||
const model = models[modelName];
|
||||
const log = model.definition.settings.log;
|
||||
|
@ -53,7 +61,7 @@ exports.translateValues = async(instance, changes) => {
|
|||
|
||||
const row = await model.findById(value, {
|
||||
fields: fieldsToShow
|
||||
});
|
||||
}, options);
|
||||
const newValue = getValue(row);
|
||||
if (newValue) finalValue = newValue;
|
||||
}
|
||||
|
@ -76,7 +84,12 @@ exports.translateValues = async(instance, changes) => {
|
|||
exports.getChanges = (original, changes) => {
|
||||
const oldChanges = {};
|
||||
const newChanges = {};
|
||||
|
||||
for (let property in changes) {
|
||||
const firstChar = property.substring(0, 1);
|
||||
const isPrivate = firstChar == '$';
|
||||
if (isPrivate) return;
|
||||
|
||||
if (changes[property] != original[property]) {
|
||||
newChanges[property] = changes[property];
|
||||
|
||||
|
|
|
@ -19,11 +19,10 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.importToNewRefundTicket = async(ctx, id) => {
|
||||
Self.importToNewRefundTicket = async(ctx, id, options) => {
|
||||
const models = Self.app.models;
|
||||
const token = ctx.req.accessToken;
|
||||
const userId = token.userId;
|
||||
const tx = await Self.beginTransaction({});
|
||||
const filter = {
|
||||
where: {id: id},
|
||||
include: [
|
||||
|
@ -63,29 +62,39 @@ module.exports = Self => {
|
|||
]
|
||||
};
|
||||
|
||||
let tx;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
let options = {transaction: tx};
|
||||
const worker = await models.Worker.findOne({
|
||||
where: {userFk: userId}
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const obsevationType = await models.ObservationType.findOne({
|
||||
where: {description: 'comercial'}
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const agencyMode = await models.AgencyMode.findOne({
|
||||
where: {code: 'refund'}
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const state = await models.State.findOne({
|
||||
where: {code: 'DELIVERED'}
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const zone = await models.Zone.findOne({
|
||||
where: {agencyModeFk: agencyMode.id}
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const claim = await models.Claim.findOne(filter, options);
|
||||
const claim = await models.Claim.findOne(filter, myOptions);
|
||||
const today = new Date();
|
||||
|
||||
const newRefundTicket = await models.Ticket.create({
|
||||
|
@ -98,33 +107,33 @@ module.exports = Self => {
|
|||
addressFk: claim.ticket().addressFk,
|
||||
agencyModeFk: agencyMode.id,
|
||||
zoneFk: zone.id
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
await saveObservation({
|
||||
description: `Reclama ticket: ${claim.ticketFk}`,
|
||||
ticketFk: newRefundTicket.id,
|
||||
observationTypeFk: obsevationType.id
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
await models.TicketTracking.create({
|
||||
ticketFk: newRefundTicket.id,
|
||||
stateFk: state.id,
|
||||
workerFk: worker.id
|
||||
}, options);
|
||||
}, myOptions);
|
||||
|
||||
const salesToRefund = await models.ClaimBeginning.find(salesFilter, options);
|
||||
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, options);
|
||||
await insertIntoClaimEnd(createdSales, id, worker.id, options);
|
||||
const salesToRefund = await models.ClaimBeginning.find(salesFilter, myOptions);
|
||||
const createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, myOptions);
|
||||
await insertIntoClaimEnd(createdSales, id, worker.id, myOptions);
|
||||
|
||||
await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [
|
||||
newRefundTicket.id, claim.ticketFk
|
||||
], options);
|
||||
], myOptions);
|
||||
|
||||
await tx.commit();
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return newRefundTicket;
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,42 +1,43 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
const LoopBackContext = require('loopback-context');
|
||||
const models = app.models;
|
||||
|
||||
describe('claimBeginning', () => {
|
||||
const claimManagerId = 72;
|
||||
let ticket;
|
||||
let refundTicketSales;
|
||||
let salesInsertedInClaimEnd;
|
||||
|
||||
const activeCtx = {
|
||||
accessToken: {userId: claimManagerId},
|
||||
};
|
||||
const ctx = {req: activeCtx};
|
||||
|
||||
afterAll(async done => {
|
||||
try {
|
||||
await app.models.Ticket.destroyById(ticket.id);
|
||||
await app.models.Ticket.rawSql(`DELETE FROM vn.orderTicket WHERE ticketFk ='${ticket.id}';`);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
describe('importToNewRefundTicket()', () => {
|
||||
it('should create a new ticket with negative sales and insert the negative sales into claimEnd', async() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||
active: activeCtx
|
||||
});
|
||||
let claimId = 1;
|
||||
ticket = await app.models.ClaimBeginning.importToNewRefundTicket(ctx, claimId);
|
||||
|
||||
refundTicketSales = await app.models.Sale.find({where: {ticketFk: ticket.id}});
|
||||
salesInsertedInClaimEnd = await app.models.ClaimEnd.find({where: {claimFk: claimId}});
|
||||
const tx = await models.Entry.beginTransaction({});
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
expect(refundTicketSales.length).toEqual(1);
|
||||
expect(refundTicketSales[0].quantity).toEqual(-5);
|
||||
expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);
|
||||
const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);
|
||||
|
||||
const refundTicketSales = await models.Sale.find({
|
||||
where: {ticketFk: ticket.id}
|
||||
}, options);
|
||||
const salesInsertedInClaimEnd = await models.ClaimEnd.find({
|
||||
where: {claimFk: claimId}
|
||||
}, options);
|
||||
|
||||
expect(refundTicketSales.length).toEqual(1);
|
||||
expect(refundTicketSales[0].quantity).toEqual(-5);
|
||||
expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
describe('entry import()', () => {
|
||||
let newEntry;
|
||||
fdescribe('entry import()', () => {
|
||||
const buyerId = 35;
|
||||
const companyId = 442;
|
||||
const travelId = 1;
|
||||
|
@ -52,29 +51,32 @@ describe('entry import()', () => {
|
|||
}
|
||||
};
|
||||
const tx = await app.models.Entry.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
const newEntry = await app.models.Entry.create({
|
||||
dated: new Date(),
|
||||
supplierFk: supplierId,
|
||||
travelFk: travelId,
|
||||
companyFk: companyId,
|
||||
observation: 'The entry',
|
||||
ref: 'Entry ref'
|
||||
}, options);
|
||||
|
||||
newEntry = await app.models.Entry.create({
|
||||
dated: new Date(),
|
||||
supplierFk: supplierId,
|
||||
travelFk: travelId,
|
||||
companyFk: companyId,
|
||||
observation: 'The entry',
|
||||
ref: 'Entry ref'
|
||||
}, options);
|
||||
await app.models.Entry.importBuys(ctx, newEntry.id, options);
|
||||
|
||||
await app.models.Entry.importBuys(ctx, newEntry.id, options);
|
||||
const updatedEntry = await app.models.Entry.findById(null, null, options);
|
||||
const entryBuys = await app.models.Buy.find({
|
||||
where: {entryFk: newEntry.id}
|
||||
}, options);
|
||||
|
||||
const updatedEntry = await app.models.Entry.findById(newEntry.id, null, options);
|
||||
const entryBuys = await app.models.Buy.find({
|
||||
where: {entryFk: newEntry.id}
|
||||
}, options);
|
||||
expect(updatedEntry.observation).toEqual(expectedObservation);
|
||||
expect(updatedEntry.ref).toEqual(expectedRef);
|
||||
expect(entryBuys.length).toEqual(2);
|
||||
|
||||
expect(updatedEntry.observation).toEqual(expectedObservation);
|
||||
expect(updatedEntry.ref).toEqual(expectedRef);
|
||||
expect(entryBuys.length).toEqual(2);
|
||||
|
||||
// Restores
|
||||
await tx.rollback();
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
"name": "Ticket",
|
||||
"base": "Loggable",
|
||||
"log": {
|
||||
"model":"TicketLog"
|
||||
"model":"TicketLog",
|
||||
"showField": "id"
|
||||
},
|
||||
"options": {
|
||||
"mysql": {
|
||||
|
|
Loading…
Reference in New Issue