5275-item.fixed-price_refactor #1426

Merged
vicent merged 12 commits from 5275-item.fixed-price_refactor into dev 2023-04-11 05:18:53 +00:00
4 changed files with 127 additions and 22 deletions
Showing only changes of commit 3b615d9879 - Show all commits

View File

@ -0,0 +1,96 @@
module.exports = Self => {
Self.remoteMethodCtx('editFixedPrice', {
description: 'Updates a column for one or more fixed price',
accessType: 'WRITE',
accepts: [{
arg: 'field',
type: 'string',
required: true,
description: `the column to edit`
},
{
arg: 'newValue',
type: 'any',
required: true,
description: `The new value to save`
},
{
arg: 'lines',
type: ['object'],
required: true,
description: `the buys which will be modified`
},
{
arg: 'filter',
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/editFixedPrice`,
verb: 'POST'
}
});
Self.editFixedPrice = async(ctx, field, newValue, lines, filter, options) => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
let modelName;
let identifier;
switch (field) {
case 'hasMinPrice':
case 'minPrice':
modelName = 'Item';
identifier = 'itemFk';
break;
case 'rate2':
case 'rate3':
case 'started':
case 'ended':
case 'warehouseFk':
modelName = 'FixedPrice';
identifier = 'id';
}
const models = Self.app.models;
const model = models[modelName];
try {
const promises = [];
const value = {};
value[field] = newValue;
if (filter) {
filter = {where: filter};
lines = await models.FixedPrice.filter(ctx, filter, myOptions);
}
const targets = lines.map(line => {
return line[identifier];
});
for (let target of targets)
promises.push(model.upsertWithWhere({id: target}, value, myOptions));
const result = await Promise.all(promises);
if (tx) await tx.commit();
return result;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -184,8 +184,7 @@ module.exports = Self => {
}
}
stmt.merge(conn.makeWhere(filter.where));
stmt.merge(conn.makePagination(filter));
stmt.merge(conn.makeSuffix(filter));
const fixedPriceIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';');

View File

@ -2,4 +2,5 @@ module.exports = Self => {
require('../methods/fixed-price/filter')(Self);
require('../methods/fixed-price/upsertFixedPrice')(Self);
require('../methods/fixed-price/getRate2')(Self);
require('../methods/fixed-price/editFixedPrice')(Self);
};

View File

@ -102,29 +102,38 @@ export default class Controller extends Section {
}
onEditAccept() {
for (const fixedPrice of this.checked) {
fixedPrice[this.editedColumn.field] = this.editedColumn.newValue;
console.log(fixedPrice);
const rowsToEdit = [];
for (let row of this.checked)
rowsToEdit.push({id: row.id, itemFk: row.itemFk});
this.upsertPrice(fixedPrice, false);
const data = {
field: this.editedColumn.field,
newValue: this.editedColumn.newValue,
lines: rowsToEdit
};
if (this.checkedDummyCount && this.checkedDummyCount > 0) {
const params = {};
if (this.$.model.userParams) {
const userParams = this.$.model.userParams;
for (let param in userParams) {
let newParam = this.exprBuilder(param, userParams[param]);
if (!newParam)
newParam = {[param]: userParams[param]};
Object.assign(params, newParam);
}
}
if (this.$.model.userFilter)
Object.assign(params, this.$.model.userFilter.where);
data.filter = params;
}
// if (this.checkedDummyCount && this.checkedDummyCount > 0) {
// const params = {};
// if (this.$.model.userParams) {
// const userParams = this.$.model.userParams;
// for (let param in userParams) {
// let newParam = this.exprBuilder(param, userParams[param]);
// if (!newParam)
// newParam = {[param]: userParams[param]};
// Object.assign(params, newParam);
// }
// }
// if (this.$.model.userFilter)
// Object.assign(params, this.$.model.userFilter.where);
// data.filter = params;
// }
return this.$http.post('FixedPrices/editFixedPrice', data)
.then(() => {
this.uncheck();
this.$.model.refresh();
});
}
isBigger(date) {