diff --git a/modules/entry/back/methods/entry/import.js b/modules/entry/back/methods/entry/importBuys.js similarity index 89% rename from modules/entry/back/methods/entry/import.js rename to modules/entry/back/methods/entry/importBuys.js index 21d4ef0e1..57877f80c 100644 --- a/modules/entry/back/methods/entry/import.js +++ b/modules/entry/back/methods/entry/importBuys.js @@ -1,8 +1,8 @@ const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { - Self.remoteMethodCtx('import', { - description: 'Imports the buys from a JSON file', + Self.remoteMethodCtx('importBuys', { + description: 'Imports the buys from a list', accessType: 'WRITE', accepts: [{ arg: 'id', @@ -11,6 +11,11 @@ module.exports = Self => { description: 'The entry id', http: {source: 'path'} }, + { + arg: 'options', + type: 'object', + description: 'Callback options', + }, { arg: 'ref', type: 'string', @@ -31,12 +36,12 @@ module.exports = Self => { root: true }, http: { - path: `/:id/import`, + path: `/:id/importBuys`, verb: 'POST' } }); - Self.import = async(ctx, id, options) => { + Self.importBuys = async(ctx, id, options = {}) => { const conn = Self.dataSource.connector; const args = ctx.args; const models = Self.app.models; diff --git a/modules/entry/back/methods/entry/importPreview.js b/modules/entry/back/methods/entry/importBuysPreview.js similarity index 80% rename from modules/entry/back/methods/entry/importPreview.js rename to modules/entry/back/methods/entry/importBuysPreview.js index 38fbcd3c7..9d6662327 100644 --- a/modules/entry/back/methods/entry/importPreview.js +++ b/modules/entry/back/methods/entry/importBuysPreview.js @@ -1,6 +1,6 @@ module.exports = Self => { - Self.remoteMethod('importPreview', { - description: '', + Self.remoteMethod('importBuysPreview', { + description: 'Calculates the preview buys for an entry import', accessType: 'READ', accepts: [{ arg: 'id', @@ -19,12 +19,12 @@ module.exports = Self => { root: true }, http: { - path: `/:id/importPreview`, + path: `/:id/importBuysPreview`, verb: 'GET' } }); - Self.importPreview = async(id, buys) => { + Self.importBuysPreview = async(id, buys) => { const models = Self.app.models; for (let buy of buys) { const packaging = await models.Packaging.findOne({ diff --git a/modules/entry/back/methods/entry/specs/import.spec.js b/modules/entry/back/methods/entry/specs/importBuys.spec.js similarity index 96% rename from modules/entry/back/methods/entry/specs/import.spec.js rename to modules/entry/back/methods/entry/specs/importBuys.spec.js index 882795065..89d2a3f35 100644 --- a/modules/entry/back/methods/entry/specs/import.spec.js +++ b/modules/entry/back/methods/entry/specs/importBuys.spec.js @@ -59,7 +59,7 @@ describe('entry import()', () => { ref: 'Entry ref' }, options); - await app.models.Entry.import(ctx, newEntry.id, options); + await app.models.Entry.importBuys(ctx, newEntry.id, options); const updatedEntry = await app.models.Entry.findById(newEntry.id, null, options); const entryBuys = await app.models.Buy.find({ diff --git a/modules/entry/back/methods/entry/specs/importBuysPreview.spec.js b/modules/entry/back/methods/entry/specs/importBuysPreview.spec.js new file mode 100644 index 000000000..481023fb1 --- /dev/null +++ b/modules/entry/back/methods/entry/specs/importBuysPreview.spec.js @@ -0,0 +1,39 @@ +const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); + +describe('entry importBuysPreview()', () => { + beforeAll(async done => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + + done(); + }); + + it('should import the buy rows', async() => { + const buys = [ + { + itemFk: 1, + buyingValue: 5.77, + description: 'Bow', + grouping: 1, + size: 1, + volume: 1200 + }, + { + itemFk: 4, + buyingValue: 2.16, + description: 'Arrow', + grouping: 1, + size: 25, + volume: 1125 + } + ]; + + const result = await app.models.Entry.importBuysPreview(1, buys); + const randomIndex = Math.floor(Math.random() * result.length); + const buy = result[randomIndex]; + + expect(buy.packageFk).toEqual('3'); + }); +}); diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index 55e226a4f..f1a22fddd 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -2,6 +2,6 @@ module.exports = Self => { require('../methods/entry/filter')(Self); require('../methods/entry/getEntry')(Self); require('../methods/entry/getBuys')(Self); - require('../methods/entry/import')(Self); - require('../methods/entry/importPreview')(Self); + require('../methods/entry/importBuys')(Self); + require('../methods/entry/importBuysPreview')(Self); }; diff --git a/modules/entry/front/buy/import/index.js b/modules/entry/front/buy/import/index.js index 2235162fe..fbf3343f8 100644 --- a/modules/entry/front/buy/import/index.js +++ b/modules/entry/front/buy/import/index.js @@ -55,7 +55,7 @@ class Controller extends Section { fetchBuys(buys) { const params = {buys}; - const query = `Entries/${this.entry.id}/importPreview`; + const query = `Entries/${this.entry.id}/importBuysPreview`; this.$http.get(query, {params}).then(res => { this.import.buys = res.data; }); @@ -63,7 +63,7 @@ class Controller extends Section { onSubmit() { const params = this.import; - const query = `Entries/${this.entry.id}/import`; + const query = `Entries/${this.entry.id}/importBuys`; return this.$http.post(query, params) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .then(() => this.$state.go('entry.card.buy.index'));