refs #4823 Now don't remove id attribute

This commit is contained in:
Guillermo Bonet 2023-05-17 12:39:43 +02:00
parent ae35a0b869
commit 5a0f4726ed
8 changed files with 61 additions and 46 deletions

View File

@ -87,96 +87,75 @@ let models = {
package: packageModel(sequelize),
};
// Foreign Keys
try {
// Remove ID atribute from models
models.characteristic.removeAttribute('id');
models.seasonalPeriod.removeAttribute('id');
models.package.removeAttribute('id');
models.botanicalName.removeAttribute('id');
models.countryOfOriginIsoCode.removeAttribute('id');
// -------------------------------
models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.seasonalPeriod.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.photo.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.packingConfiguration.hasMany(models.package, {
foreignKey: 'packingConfigurationFk',
as: 'package_Fk',
foreignKey: 'packingConfigurationId',
targetKey: 'packingConfigurationId',
});
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationFk',
as: 'packingConfiguration_Fk',
foreignKey: 'packingConfigurationId',
targetKey: 'packingConfigurationId',
});
models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.volumePrices.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
as: 'supplyLine_Fk',
targetKey: 'supplyLineId',
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
as: 'tradeItem_Fk',
targetKey: 'tradeItemId',
});
models.supplyLine.belongsTo(models.warehouses, {
foreignKey: 'warehouseId',
as: 'warehouse_Fk',
targetKey: 'warehouseId',
});
models.tradeItem.belongsTo(models.organization, {
foreignKey: 'organizationId',
as: 'Organization_Fk',
targetKey: 'organizationId',
});
models.supplyLine.belongsTo(models.organization, {
foreignKey: 'organizationId',
as: 'Organization_Fk',
targetKey: 'organizationId',
});
models.warehouses.belongsTo(models.organization, {
foreignKey: 'organizationId',
as: 'Organization_Fk',
targetKey: 'organizationId',
});
} catch (err) {

View File

@ -1,9 +1,16 @@
import { Sequelize } from 'sequelize';
const botanicalNames = {
botanicalNameId: {
type: Sequelize.STRING,
primaryKey: true,
},
name: {
type: Sequelize.STRING,
},
tradeItemId: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {

View File

@ -1,8 +1,13 @@
import { Sequelize } from 'sequelize';
const characteristics = {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
},
vbnCode: {
type: Sequelize.STRING,
primaryKey: true,
},
vbnValueCode: {
type: Sequelize.STRING,

View File

@ -1,8 +1,13 @@
import { Sequelize } from 'sequelize';
const countryOfOriginIsoCodes = {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
},
isoCode: {
type: Sequelize.STRING,
primaryKey: true,
},
};

View File

@ -1,8 +1,13 @@
import { Sequelize } from 'sequelize';
const PackageModel = {
packingConfigurationId: {
type: Sequelize.STRING,
primaryKey: true,
},
vbnPackageCode: {
type: Sequelize.INTEGER,
primaryKey: true,
},
customPackageId: {
type: Sequelize.STRING,
@ -10,7 +15,7 @@ const PackageModel = {
};
export default (sequelize) => {
const Package = sequelize.define('package', PackageModel, {
const Package = sequelize.define('tradeItem_packingConfigurations_package', PackageModel, {
timestamps: false,
freezeTableName: true,
});

View File

@ -1,12 +1,13 @@
import { Sequelize } from 'sequelize';
const photos = {
id: {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
},
url: {
type: Sequelize.STRING,
primaryKey: true,
},
type: {
type: Sequelize.STRING,

View File

@ -1,16 +1,17 @@
import { Sequelize } from 'sequelize';
const seasonalPeriod = {
id: {
type: Sequelize.INTEGER,
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
autoIncrement: true,
},
startWeek: {
type: Sequelize.INTEGER,
primaryKey: true,
},
endWeek: {
type: Sequelize.INTEGER,
primaryKey: true,
},
};

View File

@ -196,7 +196,7 @@ export async function syncSuppliers(){
let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
let timeStart = new moment();
let data = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`, null, headers)).data;
let data = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`)).data;
let suppliers = data.results;
for (let supplier of suppliers) {
@ -232,26 +232,37 @@ export async function syncSuppliers(){
*/
export async function syncConnections(){
await deleteConnections();
const spinner = ora(`Creating connections...`).start();
let spinner;
try {
let connections = await models.organization.findAll({
let connectionsInDb = await models.organization.findAll({
attributes: ['organizationId'],
where: { isConnected: true }
});
const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
let isExists = false, connectionsToPut = [];
for (let connectionInDb of connectionsInDb) {
for (let connectionInFloriday of connectionsInFloriday)
if (connectionInFloriday == connectionInDb.organizationId) {
isExists = true;
break;
}
if (!isExists) connectionsToPut.push(connectionInDb.organizationId)
isExists = false;
}
if (connectionsToPut.length) spinner = ora(`Creating connections in Floriday...`).start();
let i = 1;
for (let connection of connections) {
spinner.text = `Creating ${i++} of ${connections.length} connections...`
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.organizationId);
if (!remoteConnection)
await vnRequest('PUT', `${env.API_URL}/connections/${connection.organizationId}`);
for (let connection of connectionsToPut) {
spinner.text = `Creating ${i++} of ${connectionsToPut.length} connections in Floriday...`
await vnRequest('PUT', `${env.API_URL}/connections/${connection}`);
}
spinner.succeed();
if (spinner) spinner.succeed();
} catch (err) {
spinner.fail();
if (spinner) spinner.fail();
throw new Error(err);
}
}
@ -265,7 +276,7 @@ export async function syncTradeItems(){
attributes: ['organizationId'],
where: { isConnected: true }
});
let i = 1, x = 1;
let i = 0, x = 0;
for (let supplier of suppliers) {
try {
const params = new URLSearchParams({
@ -274,7 +285,7 @@ export async function syncTradeItems(){
let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`)).data
spinner.text = `Syncing ${i} trade items of [${x++}|${suppliers.length}] suppliers...`
if (!tradeItems.length) continue;
if (!tradeItems.length) continue;
for (let tradeItem of tradeItems) {
await insertItem(tradeItem);
@ -422,7 +433,7 @@ export async function insertItem(tradeItem) {
await models.package.upsert({
...packingConfiguration.package,
packingConfigurationFk: uuid,
packingConfigurationId: uuid,
}, { transaction: tx });
}
@ -441,6 +452,7 @@ export async function insertItem(tradeItem) {
if (tradeItem.botanicalNames.length)
for (const botanicalName of tradeItem.botanicalNames) {
await models.botanicalName.upsert({
botanicalNameId: uuidv4(),
name: botanicalName,
tradeItemId: tradeItem.tradeItemId,
}, { transaction: tx });
@ -463,7 +475,7 @@ export async function syncWarehouses(){
where: { isConnected: true }
});
let x = 0, i = 1;
let x = 0, i = 0;
for (let supplier of suppliers) {
spinner.text = `Syncing ${i} warehouses of [${x++}|${suppliers.length}] suppliers...`
const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${supplier.organizationId}/warehouses`)).data;
@ -517,7 +529,7 @@ export async function deleteConnections() {
spinner = ora(`Deleting connections that aren't in the db...`).start();
for (let connection of ghostConnections) {
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`, null, headers);
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`);
spinner.text = `Deleting ${i++} of ${ghostConnections.length} that aren't in the db...`
}
if (spinner) spinner.succeed();