From dc6c8f924d902425aad22b959028ab0a039d4f39 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Mon, 17 Oct 2022 07:25:25 +0200 Subject: [PATCH] feat: Create new Supplier --- .../10491-august/00-newSupplier_ACL.sql | 2 + .../00-newSupplier_Modify_supplier.sql | 5 ++ modules/supplier/back/methods/supplier/new.js | 28 ----------- .../back/methods/supplier/newSupplier.js | 50 +++++++++++++++++++ modules/supplier/back/models/supplier.js | 11 ++-- modules/supplier/front/create/index.html | 2 +- modules/supplier/front/create/index.js | 14 ++++-- modules/supplier/front/routes.json | 12 ++--- 8 files changed, 83 insertions(+), 41 deletions(-) create mode 100644 db/changes/10491-august/00-newSupplier_ACL.sql create mode 100644 db/changes/10491-august/00-newSupplier_Modify_supplier.sql delete mode 100644 modules/supplier/back/methods/supplier/new.js create mode 100644 modules/supplier/back/methods/supplier/newSupplier.js diff --git a/db/changes/10491-august/00-newSupplier_ACL.sql b/db/changes/10491-august/00-newSupplier_ACL.sql new file mode 100644 index 0000000000..f23f3026bc --- /dev/null +++ b/db/changes/10491-august/00-newSupplier_ACL.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model,property,accessType,principalId) + VALUES ('Supplier','newSupplier','WRITE','administrative'); diff --git a/db/changes/10491-august/00-newSupplier_Modify_supplier.sql b/db/changes/10491-august/00-newSupplier_Modify_supplier.sql new file mode 100644 index 0000000000..d64cd3663a --- /dev/null +++ b/db/changes/10491-august/00-newSupplier_Modify_supplier.sql @@ -0,0 +1,5 @@ +ALTER TABLE vn.supplier MODIFY COLUMN payMethodFk tinyint(3) unsigned DEFAULT NULL NULL; + +/* ------------------------------------------------------------------------------------------- */ + +ALTER TABLE vn.supplier MODIFY COLUMN supplierActivityFk varchar(45) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL NULL; \ No newline at end of file diff --git a/modules/supplier/back/methods/supplier/new.js b/modules/supplier/back/methods/supplier/new.js deleted file mode 100644 index 9fb8075327..0000000000 --- a/modules/supplier/back/methods/supplier/new.js +++ /dev/null @@ -1,28 +0,0 @@ -/* eslint-disable no-console */ -// eslint-disable-next-line no-unused-vars -let UserError = require('vn-loopback/util/user-error'); - -module.exports = Self => { - Self.remoteMethod('new', { - description: 'returns the created item', - accessType: 'WRITE', - accepts: [{ - arg: 'params', - type: 'object', - http: {source: 'body'} - }], - returns: { - type: 'number', - root: true - }, - http: { - path: `/new`, - verb: 'POST' - } - }); - - Self.new = async(params, options) => { - console.log(params); - console.log(options); - }; -}; diff --git a/modules/supplier/back/methods/supplier/newSupplier.js b/modules/supplier/back/methods/supplier/newSupplier.js new file mode 100644 index 0000000000..9fe47fd53e --- /dev/null +++ b/modules/supplier/back/methods/supplier/newSupplier.js @@ -0,0 +1,50 @@ +/* eslint-disable no-console */ +// eslint-disable-next-line no-unused-vars + +module.exports = Self => { + Self.remoteMethod('newSupplier', { + description: 'returns the created item', + accessType: 'WRITE', + accepts: [{ + arg: 'params', + type: 'object', + http: {source: 'body'} + }], + returns: { + type: 'string', + root: true + }, + http: { + path: `/newSupplier`, + verb: 'POST' + } + }); + + Self.newSupplier = async(params, options) => { + const models = Self.app.models; + const myOptions = {}; + + params.payDemFk = 1; + + console.log(params); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const supplier = await models.Supplier.create(params, myOptions); + + if (tx) await tx.commit(); + + return { + supplier + }; + } catch (e) { + console.log(e); + if (tx) await tx.rollback(); + return params; + } + }; +}; diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index a2831845a3..6edfe274cb 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -10,7 +10,7 @@ module.exports = Self => { require('../methods/supplier/freeAgencies')(Self); require('../methods/supplier/campaignMetricsPdf')(Self); require('../methods/supplier/campaignMetricsEmail')(Self); - require('../methods/supplier/new')(Self); + require('../methods/supplier/newSupplier')(Self); Self.validatesPresenceOf('name', { message: 'The social name cannot be empty' @@ -58,6 +58,9 @@ module.exports = Self => { } async function tinIsValid(err, done) { + if (!this.countryFk) + return done(); + const filter = { fields: ['code'], where: {id: this.countryFk} @@ -81,6 +84,7 @@ module.exports = Self => { }); async function hasSupplierAccount(err, done) { + if (!this.payMethodFk) return done(); const payMethod = await Self.app.models.PayMethod.findById(this.payMethodFk); const supplierAccount = await Self.app.models.SupplierAccount.findOne({where: {supplierFk: this.id}}); const hasIban = supplierAccount && supplierAccount.iban; @@ -93,6 +97,7 @@ module.exports = Self => { } Self.observe('before save', async function(ctx) { + if (ctx.isNewInstance) return; const loopbackContext = LoopBackContext.getCurrentContext(); const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; @@ -102,7 +107,7 @@ module.exports = Self => { const isPayMethodChecked = changes.isPayMethodChecked || orgData.isPayMethodChecked; const hasChanges = orgData && changes; const isPayMethodCheckedChanged = hasChanges - && orgData.isPayMethodChecked != isPayMethodChecked; + && orgData.isPayMethodChecked != isPayMethodChecked; if (isNotFinancial && isPayMethodCheckedChanged) throw new UserError('You can not modify is pay method checked'); @@ -115,7 +120,7 @@ module.exports = Self => { const socialName = changes.name || orgData.name; const hasChanges = orgData && changes; const socialNameChanged = hasChanges - && orgData.socialName != socialName; + && orgData.socialName != socialName; if ((socialNameChanged) && !isAlpha(socialName)) throw new UserError('The social name has an invalid format'); diff --git a/modules/supplier/front/create/index.html b/modules/supplier/front/create/index.html index 71fe3b4888..17e4241151 100644 --- a/modules/supplier/front/create/index.html +++ b/modules/supplier/front/create/index.html @@ -1,6 +1,6 @@ diff --git a/modules/supplier/front/create/index.js b/modules/supplier/front/create/index.js index 8708aacd67..01b02179b8 100644 --- a/modules/supplier/front/create/index.js +++ b/modules/supplier/front/create/index.js @@ -5,14 +5,22 @@ import Section from 'salix/components/section'; class Controller extends Section { constructor($element, $) { super($element, $); - console.log($); } onSubmit() { this.$.watcher.submit().then( - console.log('abc'), - json => this.$state.go('item.card.basicData', {id: json.data.id}) + json => { + console.log('abc'); + this.$state.go('item.card.basicData', {id: json.data.id}); + console.log('ctrl --> ', this.$.$ctrl); + redirect(this.$.$ctrl); + + async function redirect(ctrl) { + await new Promise(r => setTimeout(r, 100)); + window.location.href = `/#!/supplier/${ctrl.supplier.supplier.id}/fiscal-data`; + } + } ); } } diff --git a/modules/supplier/front/routes.json b/modules/supplier/front/routes.json index 03efea0f3d..8c9b7f760b 100644 --- a/modules/supplier/front/routes.json +++ b/modules/supplier/front/routes.json @@ -31,12 +31,6 @@ "component": "vn-supplier", "description": "Suppliers" }, - { - "url": "/create", - "state": "supplier.create", - "component": "vn-supplier-create", - "description": "New supplier" - }, { "url": "/index?q", "state": "supplier.index", @@ -57,6 +51,12 @@ "params": { "supplier": "$ctrl.supplier" } + }, + { + "url": "/create", + "state": "supplier.create", + "component": "vn-supplier-create", + "description": "New supplier" }, { "url": "/basic-data",