Added params to ticket method & updated unit tests
gitea/salix/1935-ticket_create_auto_fill This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-12-26 07:12:34 +01:00
parent 0799e58558
commit 5273e90a30
7 changed files with 209 additions and 132 deletions

View File

@ -18,7 +18,6 @@ module.exports = Self => {
}); });
Self.regularizeClaim = async(ctx, params) => { Self.regularizeClaim = async(ctx, params) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const resolvedState = 3; const resolvedState = 3;
@ -55,11 +54,10 @@ module.exports = Self => {
if (!ticketFk) { if (!ticketFk) {
ticketFk = await createTicket(ctx, { ticketFk = await createTicket(ctx, {
clientFk: address.clientFk, clientId: address.clientFk,
addressFk: addressFk, warehouseId: sale.ticket().warehouseFk,
warehouseFk: sale.ticket().warehouseFk, companyId: sale.ticket().companyFk,
companyFk: sale.ticket().companyFk, addressId: addressFk
userId: userId
}, options); }, options);
} }
@ -140,16 +138,21 @@ module.exports = Self => {
} }
async function createTicket(ctx, params, options) { async function createTicket(ctx, params, options) {
let ticket = await Self.app.models.Ticket.new(ctx, params.shipped = new Date();
{ params.landed = new Date();
shipped: new Date(), params.agencyModeId = null;
landed: new Date(), params.routeId = null;
clientFk: params.clientFk,
warehouseFk: params.warehouseFk, const ticket = await Self.app.models.Ticket.new(ctx,
companyFk: params.companyFk, params.clientId,
addressFk: params.addressFk, params.shipped,
userId: params.userId params.landed,
}, options); params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
return ticket.id; return ticket.id;
} }

View File

@ -11,7 +11,7 @@
</section> </section>
<vn-data-viewer <vn-data-viewer
model="model" model="model"
class="vn-w-lg"> class="vn-w-xl">
<vn-horizontal class="photo-list"> <vn-horizontal class="photo-list">
<section class="photo" ng-repeat="photo in $ctrl.photos"> <section class="photo" ng-repeat="photo in $ctrl.photos">
<section class="image vn-shadow" on-error-src <section class="image vn-shadow" on-error-src

View File

@ -29,7 +29,6 @@ module.exports = Self => {
}); });
Self.regularize = async(ctx, itemFk, quantity, warehouseFk) => { Self.regularize = async(ctx, itemFk, quantity, warehouseFk) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const itemDestination = await models.ClaimDestination.findOne({ const itemDestination = await models.ClaimDestination.findOne({
@ -56,10 +55,9 @@ module.exports = Self => {
if (!ticketFk) { if (!ticketFk) {
ticketFk = await createTicket(ctx, { ticketFk = await createTicket(ctx, {
clientFk: itemDestination.address().clientFk, clientId: itemDestination.address().clientFk,
addressFk: itemDestination.addressFk, warehouseId: warehouseFk,
warehouseFk: warehouseFk, addressId: itemDestination.addressFk
userId: userId
}, options); }, options);
} }
@ -88,17 +86,22 @@ module.exports = Self => {
} }
async function createTicket(ctx, params, options) { async function createTicket(ctx, params, options) {
let ticket = await Self.app.models.Ticket.new( params.shipped = new Date();
ctx, params.landed = new Date();
{ params.companyId = null;
shipped: new Date(), params.agencyModeId = null;
landed: new Date(), params.routeId = null;
clientFk: params.clientFk,
warehouseFk: params.warehouseFk, const ticket = await Self.app.models.Ticket.new(ctx,
companyFk: params.companyFk, params.clientId,
addressFk: params.addressFk, params.shipped,
userId: params.userId params.landed,
}, options); params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
return ticket.id; return ticket.id;
} }

View File

@ -2,14 +2,50 @@ let UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('new', { Self.remoteMethodCtx('new', {
description: 'Create a newticket and returns the new ID', description: 'Creates a new ticket and returns the id',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
arg: 'params', arg: 'clientId',
type: 'object', type: 'Number',
required: true, description: `The client id filter`,
description: 'ClientFk, Shipped, WarehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId', required: true
http: {source: 'body'} },
{
arg: 'shipped',
type: 'Date',
description: `The shipment date filter`
},
{
arg: 'landed',
type: 'Date',
description: `The landing date filter`
},
{
arg: 'warehouseId',
type: 'Number',
description: `The warehouse id filter`,
required: true
},
{
arg: 'companyId',
type: 'Number',
description: `The company id filter`
},
{
arg: 'addressId',
type: 'Number',
description: `The address id filter`,
required: true
},
{
arg: 'agencyModeId',
type: 'Number',
description: `The agencyMode id filter`
},
{
arg: 'routeId',
type: 'Number',
description: `The route id filter`
}], }],
returns: { returns: {
type: 'number', type: 'number',
@ -21,10 +57,12 @@ module.exports = Self => {
} }
}); });
Self.new = async(ctx, params, options) => { Self.new = async(ctx, clientId, shipped, landed, warehouseId,
let models = Self.app.models; companyId, addressId, agencyModeId, routeId, options) => {
let address = await models.Address.findOne({ const myUserId = ctx.req.accessToken.userId;
where: {id: params.addressFk}, const models = Self.app.models;
const address = await models.Address.findOne({
where: {id: addressId},
fields: ['id', 'clientFk'], fields: ['id', 'clientFk'],
include: { include: {
relation: 'client', relation: 'client',
@ -36,18 +74,12 @@ module.exports = Self => {
} }
}); });
// FIXME: #1953 Params should be declared with it's correct argument type
if (params.shipped)
params.shipped = new Date(params.shipped);
if (params.landed)
params.landed = new Date(params.landed);
if (!address) if (!address)
throw new UserError(`This address doesn't exist`); throw new UserError(`This address doesn't exist`);
let agencyMode; let agencyMode;
if (params && params.agencyModeFk) if (agencyModeId)
agencyMode = await models.AgencyMode.findById(params.agencyModeFk); agencyMode = await models.AgencyMode.findById(agencyModeId);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) { if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
if (!address.client().isActive) if (!address.client().isActive)
@ -65,33 +97,30 @@ module.exports = Self => {
} }
try { try {
if (!params.shipped && params.landed) { if (!shipped && landed) {
const shippedResult = await models.Agency.getShipped(params.landed, const shippedResult = await models.Agency.getShipped(landed,
address.id, params.agencyModeFk, params.warehouseFk); address.id, agencyModeId, warehouseId);
params.shipped = shippedResult && shippedResult.shipped; shipped = shippedResult && shippedResult.shipped;
} }
if (params.shipped && !params.landed) { if (shipped && !landed) {
const landedResult = await models.Agency.getLanded(params.shipped, const landedResult = await models.Agency.getLanded(shipped,
address.id, params.agencyModeFk, params.warehouseFk); address.id, agencyModeId, warehouseId);
params.landed = landedResult && landedResult.landed; landed = landedResult && landedResult.landed;
} }
if (!params.userId && ctx.req && ctx.req.accessToken.userId)
params.userId = ctx.req.accessToken.userId;
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result); query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`; SELECT @result newTicketId;`;
let result = await Self.rawSql(query, [ let result = await Self.rawSql(query, [
params.clientFk, clientId,
params.shipped, shipped,
params.warehouseFk, warehouseId,
params.companyFk || 442, companyId || 442,
params.addressFk, addressId,
params.agencyModeFk || null, agencyModeId || null,
params.routeFk || null, routeId || null,
params.landed, landed,
params.userId myUserId
], options); ], options);
let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options); let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options);
@ -99,7 +128,7 @@ module.exports = Self => {
let logRecord = { let logRecord = {
originFk: cleanInstance.id, originFk: cleanInstance.id,
userFk: params.userId, userFk: myUserId,
action: 'create', action: 'create',
changedModel: 'Ticket', changedModel: 'Ticket',
changedModelId: cleanInstance.id, changedModelId: cleanInstance.id,

View File

@ -15,42 +15,74 @@ describe('ticket new()', () => {
it('should throw an error if the client isnt frozen and isnt active', async() => { it('should throw an error if the client isnt frozen and isnt active', async() => {
let error; let error;
let params = {addressFk: 6}; let params = {
clientId: 106,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 6
};
await app.models.Ticket.new(ctx, params) await app.models.Ticket.new(ctx,
.catch(e => { params.clientId,
error = e; params.shipped,
}); params.landed,
params.warehouseId,
params.companyFk,
params.addressId
).catch(e => {
error = e;
});
expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`)); expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`));
}); });
it('should throw an error if the address doesnt exist', async() => { it('should throw an error if the address doesnt exist', async() => {
let error; let error;
let params = {addressFk: 'invalid address', clientFk: 104}; let params = {
clientId: 104,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 'invalid address'
};
await app.models.Ticket.new(ctx, params) await app.models.Ticket.new(ctx,
.catch(response => { params.clientId,
expect(response.message).toEqual(`This address doesn't exist`); params.shipped,
error = response; params.landed,
}); params.warehouseId,
params.companyFk,
params.addressId
).catch(response => {
expect(response.message).toEqual(`This address doesn't exist`);
error = response;
});
expect(error).toBeDefined(); expect(error).toBeDefined();
}); });
it('should return the id of the created ticket', async() => { it('should return the id of the created ticket', async() => {
let params = { let params = {
warehouseFk: 1, clientId: 104,
clientFk: 104,
companyFk: 442,
addressFk: 4,
agencyModeFk: 1,
userId: 9,
shipped: today, shipped: today,
landed: today landed: today,
warehouseId: 1,
companyId: 442,
addressId: 4,
agencyModeId: 1
}; };
ticket = await app.models.Ticket.new(ctx, params); ticket = await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyFk,
params.addressId,
params.agencyModeId);
let newestTicketIdInFixtures = 21; let newestTicketIdInFixtures = 21;

View File

@ -1,20 +1,19 @@
<vn-autocomplete <vn-autocomplete vn-focus
vn-focus
vn-id="client" vn-id="client"
url="Clients" url="Clients"
label="Client" label="Client"
search-function="{or: [{id: $search}, {name: {like: '%'+ $search +'%'}}]}" search-function="{or: [{id: $search}, {name: {like: '%'+ $search +'%'}}]}"
show-field="name" show-field="name"
value-field="id" value-field="id"
ng-model="$ctrl.clientFk" ng-model="$ctrl.clientId"
order="id"> order="id">
<tpl-item>{{id}}: {{name}}</tpl-item> <tpl-item>{{id}}: {{name}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete
disabled="!$ctrl.clientFk" disabled="!$ctrl.clientId"
url="{{ $ctrl.clientFk ? 'Clients/'+ $ctrl.clientFk +'/addresses' : null }}" url="{{ $ctrl.clientId ? 'Clients/'+ $ctrl.clientId +'/addresses' : null }}"
fields="['nickname', 'street', 'city']" fields="['nickname', 'street', 'city']"
ng-model="$ctrl.addressFk" ng-model="$ctrl.addressId"
show-field="nickname" show-field="nickname"
value-field="id" value-field="id"
label="Address"> label="Address">
@ -25,18 +24,18 @@
ng-model="$ctrl.landed"> ng-model="$ctrl.landed">
</vn-date-picker> </vn-date-picker>
<vn-autocomplete <vn-autocomplete
disabled="!$ctrl.warehouseFk && (!$ctrl.clientFk || !$ctrl.landed)" disabled="!$ctrl.warehouseId && (!$ctrl.clientId || !$ctrl.landed)"
ng-model="$ctrl.warehouseFk" ng-model="$ctrl.warehouseId"
url="Warehouses" url="Warehouses"
show-field="name" show-field="name"
value-field="id" value-field="id"
label="Warehouse"> label="Warehouse">
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete
disabled="!$ctrl.clientFk || !$ctrl.landed || !$ctrl.warehouseFk" disabled="!$ctrl.clientId || !$ctrl.landed || !$ctrl.warehouseId"
data="$ctrl._availableAgencies" data="$ctrl._availableAgencies"
label="Agency" label="Agency"
show-field="agencyMode" show-field="agencyMode"
value-field="agencyModeFk" value-field="agencyModeFk"
ng-model="$ctrl.ticket.agencyModeFk"> ng-model="$ctrl.agencyModeId">
</vn-autocomplete> </vn-autocomplete>

View File

@ -19,16 +19,20 @@ class Controller {
this.warehouseFk = this.vnConfig.warehouseFk; this.warehouseFk = this.vnConfig.warehouseFk;
} }
get ticket() {
return this._ticket;
}
set ticket(value) { set ticket(value) {
if (value) if (value)
this._ticket = value; this._ticket = value;
} }
get ticket() { get clientId() {
return this._ticket; return this.ticket.clientFk;
} }
set clientFk(value) { set clientId(value) {
this.ticket.clientFk = value; this.ticket.clientFk = value;
if (value) { if (value) {
@ -36,7 +40,7 @@ class Controller {
include: { include: {
relation: 'defaultAddress', relation: 'defaultAddress',
scope: { scope: {
fields: 'id' fields: ['id', 'agencyModeFk']
} }
}, },
where: {id: value} where: {id: value}
@ -44,32 +48,21 @@ class Controller {
filter = encodeURIComponent(JSON.stringify(filter)); filter = encodeURIComponent(JSON.stringify(filter));
let query = `Clients?filter=${filter}`; let query = `Clients?filter=${filter}`;
this.$http.get(query).then(res => { this.$http.get(query).then(res => {
if (res.data) { const [client] = res.data;
let client = res.data[0]; this.defaultAddress = client.defaultAddress;
let defaultAddress = client.defaultAddress; this.addressId = this.defaultAddress.id;
this.addressFk = defaultAddress.id;
}
}); });
} else } else
this.addressFk = null; this.addressId = null;
this.getAvailableAgencies(); this.getAvailableAgencies();
} }
get clientFk() { get addressId() {
return this.ticket.clientFk;
}
set addressFk(value) {
this.ticket.addressFk = value;
this.getAvailableAgencies();
}
get addressFk() {
return this.ticket.addressFk; return this.ticket.addressFk;
} }
set landed(value) { set addressId(value) {
this.ticket.landed = value; this.ticket.addressFk = value;
this.getAvailableAgencies(); this.getAvailableAgencies();
} }
@ -77,12 +70,26 @@ class Controller {
return this.ticket.landed; return this.ticket.landed;
} }
set warehouseFk(value) { set landed(value) {
this.ticket.landed = value;
this.getAvailableAgencies();
}
get warehouseId() {
return this.ticket.warehouseFk;
}
set warehouseId(value) {
this.ticket.warehouseFk = value; this.ticket.warehouseFk = value;
this.getAvailableAgencies(); this.getAvailableAgencies();
} }
get warehouseFk() {
return this.ticket.warehouseFk; get agencyModeId() {
return this.ticket.agencyModeFk;
}
set agencyModeId(value) {
this.ticket.agencyModeFk = value;
} }
getAvailableAgencies() { getAvailableAgencies() {
@ -96,8 +103,12 @@ class Controller {
if (params.warehouseFk && params.addressFk && params.landed) { if (params.warehouseFk && params.addressFk && params.landed) {
ticket.agencyModeFk = null; ticket.agencyModeFk = null;
this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params}) this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params}).then(res => {
.then(res => this._availableAgencies = res.data); this._availableAgencies = res.data;
this.agencyModeId = this.defaultAddress.agencyModeFk;
console.log(this.agencyModeId);
});
} }
} }
@ -107,11 +118,11 @@ class Controller {
createTicket() { createTicket() {
let params = { let params = {
clientFk: this.ticket.clientFk, clientId: this.ticket.clientFk,
landed: this.ticket.landed, landed: this.ticket.landed,
addressFk: this.ticket.addressFk, addressId: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk, agencyModeId: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk, warehouseId: this.ticket.warehouseFk,
}; };
this.$http.post(`Tickets/new`, params).then(res => { this.$http.post(`Tickets/new`, params).then(res => {
this.vnApp.showSuccess(this.translate.instant('Data saved!')); this.vnApp.showSuccess(this.translate.instant('Data saved!'));