ipa-customer-settings
Advanced tools
Comparing version 0.0.27 to 0.0.28
{ | ||
"name": "ipa-customer-settings", | ||
"version": "0.0.27", | ||
"version": "0.0.28", | ||
"lockfileVersion": 1, | ||
@@ -5,0 +5,0 @@ "requires": true, |
{ | ||
"name": "ipa-customer-settings", | ||
"version": "0.0.27", | ||
"version": "0.0.28", | ||
"author": "OpusCapita", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
'use strict'; | ||
const lodash = require('lodash'); | ||
class CustomerSettingsApi { | ||
@@ -25,2 +28,14 @@ static getInstance(db) { | ||
static getCustomerSettingsDifference(from, to) { | ||
return lodash.differenceWith( | ||
from, | ||
to, | ||
(fromSettings, toSettings) => ( | ||
(fromSettings.customerId === toSettings.customerId) && | ||
(fromSettings.settingName === toSettings.settingName) | ||
) | ||
); | ||
} | ||
async findById(id) { | ||
@@ -59,9 +74,15 @@ const setting = await this.db.models.CustomerSetting.findById(id); | ||
const customerId = customerSettings[0] ? customerSettings[0].customerId : null; | ||
const actualCustomerSettings = this.findAll({ where: { customerId } }); | ||
const itemsToRemove = CustomerSettingsApi.getCustomerSettingsDifference(actualCustomerSettings, customerSettings); | ||
const transaction = await this.db.transaction(); | ||
try { | ||
const items = []; | ||
if (customerSettings.length) { | ||
return Promise.all( | ||
await Promise.all( | ||
customerSettings.map(async item => { | ||
await this.db.models.CustomerSetting.upsert(item); | ||
return this.db.models.CustomerSetting.find({ | ||
const newItem = await this.db.models.CustomerSetting.find({ | ||
where: { | ||
@@ -72,6 +93,14 @@ customerId: item.customerId, | ||
}); | ||
}) | ||
if (newItem) { | ||
items.push(newItem); | ||
} | ||
}).concat( | ||
itemsToRemove.length ? | ||
itemsToRemove.map(item => this.removeById(item.id, transaction)) : | ||
Promise.resolve() | ||
) | ||
); | ||
} | ||
await transaction.commit(); | ||
return items; | ||
} catch (err) { | ||
@@ -81,4 +110,2 @@ await transaction.rollback(); | ||
} | ||
return Promise.resolve(); | ||
} | ||
@@ -85,0 +112,0 @@ |
'use strict'; | ||
const createCustomerSettings = require('./customer-settings/createCustomerSettings'); | ||
const copyCustomerSettings = require('./customer-settings/copyCustomerSettings'); | ||
const dropCustomerSettings = require('./customer-settings/dropCustomerSettings'); | ||
@@ -9,2 +10,3 @@ | ||
await createCustomerSettings(db.queryInterface, config); | ||
await copyCustomerSettings(db.queryInterface, { tablePrefixFrom: 'IPA', tablePrefixTo: config.tablePrefix }); | ||
}, | ||
@@ -11,0 +13,0 @@ down: async (db, config) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81962
18
2295