refs #4823 Refactor and change in table names

This commit is contained in:
Guillermo Bonet 2023-05-30 11:46:46 +02:00
parent 52d2e783c8
commit 9aaf5a9485
16 changed files with 117 additions and 115 deletions

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const clientConfig = {
const config = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
@ -21,13 +21,12 @@ const clientConfig = {
};
export default (sequelize) => {
const ClientConfig = sequelize.define(
'clientConfig',
clientConfig,
return sequelize.define(
'config',
config,
{
timestamps: false,
freezeTableName: true,
}
);
return ClientConfig;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const organizations = {
const organization = {
organizationId: {
type: Sequelize.STRING,
allowNull: false,
@ -60,12 +60,11 @@ const organizations = {
};
export default (sequelize) => {
const Organizations = sequelize.define(
return sequelize.define(
'organization',
organizations, {
organization, {
timestamps: false,
freezeTableName: true,
}
);
return Organizations;
};

View File

@ -30,27 +30,29 @@ try {
}
// Conf Models
import clientConfig from './config/clientConfig.js';
import config from './config/config.js';
import sequenceNumber from './config/sequenceNumber.js';
// Supply Line Models
import supplyLine from './supplyLine/supplyLine.js';
import volumePrices from './supplyLine/volumePrices.js';
import clockPresalesSupply from './supplyLine/clockPresalesSupply.js';
// Organization Models
import organization from './organization/organization.js';
import warehouses from './organization/warehouses.js';
// Warehouse Models
import warehouse from './warehouse/warehouse.js';
// Supply Line Models
import supplyLine from './supplyLine/supplyLine.js';
import volumePric from './supplyLine/volumePrice.js';
import clockPresaleSupply from './supplyLine/clockPresaleSupply.js';
// TradeItem Models
import tradeItem from './tradeItem/tradeItem.js';
import botanicalNames from './tradeItem/botanicalNames.js';
import countryOfOriginIsoCodes from './tradeItem/countryOfOriginIsoCodes.js';
import botanicalName from './tradeItem/botanicalName.js';
import countryOfOriginIsoCode from './tradeItem/countryOfOriginIsoCode.js';
import packageModel from './tradeItem/package.js';
import packingConfigurations from './tradeItem/packingConfigurations.js';
import photos from './tradeItem/photos.js';
import packingConfiguration from './tradeItem/packingConfiguration.js';
import photo from './tradeItem/photo.js';
import seasonalPeriod from './tradeItem/seasonalPeriod.js';
import characteristics from './tradeItem/characteristics.js';
import characteristic from './tradeItem/characteristic.js';
/**
* Contains all the models that are related to the application.
@ -58,24 +60,28 @@ import characteristics from './tradeItem/characteristics.js';
let models = {
sequelize: sequelize,
sequenceNumber: sequenceNumber(sequelize),
clientConfig: clientConfig(sequelize),
config: config(sequelize),
organization: organization(sequelize),
warehouses: warehouses(sequelize),
warehouse: warehouse(sequelize),
tradeItem: tradeItem(sequelize),
botanicalName: botanicalNames(sequelize),
characteristic: characteristics(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCodes(sequelize),
packingConfiguration: packingConfigurations(sequelize),
photo: photos(sequelize),
botanicalName: botanicalName(sequelize),
characteristic: characteristic(sequelize),
countryOfOriginIsoCode: countryOfOriginIsoCode(sequelize),
packingConfiguration: packingConfiguration(sequelize),
photo: photo(sequelize),
seasonalPeriod: seasonalPeriod(sequelize),
supplyLine: supplyLine(sequelize),
volumePrices: volumePrices(sequelize),
clockPresalesSupply: clockPresalesSupply(sequelize),
volumePrice: volumePric(sequelize),
clockPresaleSupply: clockPresaleSupply(sequelize),
package: packageModel(sequelize),
};
// Foreign Keys
try {
/* TODO: Aplicar dependiendo de cada caso
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
*/
models.characteristic.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
@ -99,31 +105,24 @@ try {
models.package.belongsTo(models.packingConfiguration, {
foreignKey: 'packingConfigurationId',
targetKey: 'packingConfigurationId',
});
models.botanicalName.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
models.countryOfOriginIsoCode.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
models.volumePrices.belongsTo(models.supplyLine, {
models.volumePrice.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
targetKey: 'supplyLineId',
});
models.supplyLine.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
models.supplyLine.belongsTo(models.warehouses, {
models.supplyLine.belongsTo(models.warehouse, {
foreignKey: 'warehouseId',
targetKey: 'warehouseId',
});
@ -135,19 +134,19 @@ try {
foreignKey: 'organizationId',
targetKey: 'organizationId',
});
models.warehouses.belongsTo(models.organization, {
models.warehouse.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
});
models.clockPresalesSupply.belongsTo(models.supplyLine, {
models.clockPresaleSupply.belongsTo(models.supplyLine, {
foreignKey: 'supplyLineId',
targetKey: 'supplyLineId',
});
models.clockPresalesSupply.belongsTo(models.tradeItem, {
models.clockPresaleSupply.belongsTo(models.tradeItem, {
foreignKey: 'tradeItemId',
targetKey: 'tradeItemId',
});
models.clockPresalesSupply.belongsTo(models.organization, {
models.clockPresaleSupply.belongsTo(models.organization, {
foreignKey: 'organizationId',
targetKey: 'organizationId',
});

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const clockPresalesSupply = {
const clockPresaleSupply = {
supplyLineId: {
type: Sequelize.STRING,
primaryKey: true,
@ -54,8 +54,8 @@ const clockPresalesSupply = {
export default (sequelize) => {
return sequelize.define(
'supplyLine_clockPresalesSupply',
clockPresalesSupply, {
'supplyLineClockPresaleSupply',
clockPresaleSupply, {
timestamps: false,
freezeTableName: true,
}

View File

@ -75,12 +75,11 @@ const supplyLine = {
};
export default (sequelize) => {
const SupplyLine = sequelize.define(
return sequelize.define(
'supplyLine',
supplyLine, {
timestamps: false,
freezeTableName: true,
}
);
return SupplyLine;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const volumePrices = {
const volumePrice = {
supplyLineId: {
type: Sequelize.STRING,
primaryKey: true,
@ -16,12 +16,11 @@ const volumePrices = {
};
export default (sequelize) => {
const VolumePrices = sequelize.define(
'supplyLine_volumePrices',
volumePrices, {
return sequelize.define(
'supplyLineVolumePrice',
volumePrice, {
timestamps: false,
freezeTableName: true,
}
);
return VolumePrices;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const botanicalNames = {
const botanicalName = {
botanicalNameId: {
type: Sequelize.STRING,
primaryKey: true,
@ -14,12 +14,11 @@ const botanicalNames = {
};
export default (sequelize) => {
const BotanicalNames = sequelize.define(
'tradeItem_botanicalNames',
botanicalNames, {
return sequelize.define(
'tradeItemBotanicalName',
botanicalName, {
timestamps: false,
freezeTableName: true,
}
);
return BotanicalNames;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const characteristics = {
const characteristic = {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
@ -15,13 +15,12 @@ const characteristics = {
};
export default (sequelize) => {
const Characteristics = sequelize.define(
'tradeItem_characteristics',
characteristics,
return sequelize.define(
'tradeItemCharacteristic',
characteristic,
{
timestamps: false,
freezeTableName: true,
}
);
return Characteristics;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const countryOfOriginIsoCodes = {
const countryOfOriginIsoCode = {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
@ -12,13 +12,12 @@ const countryOfOriginIsoCodes = {
};
export default (sequelize) => {
const CountryOfOriginIsoCodes = sequelize.define(
'tradeItem_countryOfOriginIsoCodes',
countryOfOriginIsoCodes,
return sequelize.define(
'tradeItemCountryOfOriginIsoCodes',
countryOfOriginIsoCode,
{
timestamps: false,
freezeTableName: true,
}
);
return CountryOfOriginIsoCodes;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const PackageModel = {
const packageModel = {
packingConfigurationId: {
type: Sequelize.STRING,
primaryKey: true,
@ -15,9 +15,12 @@ const PackageModel = {
};
export default (sequelize) => {
const Package = sequelize.define('tradeItem_packingConfigurations_package', PackageModel, {
return sequelize.define(
'tradeItemPackingConfigurationPackage',
packageModel,
{
timestamps: false,
freezeTableName: true,
});
return Package;
}
);
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const packingConfigurations = {
const packingConfiguration = {
packingConfigurationId: {
type: Sequelize.STRING,
primaryKey: true,
@ -38,13 +38,12 @@ const packingConfigurations = {
};
export default (sequelize) => {
const PackingConfigurations = sequelize.define(
'tradeItem_packingConfigurations',
packingConfigurations,
return sequelize.define(
'tradeItemPackingConfiguration',
packingConfiguration,
{
timestamps: false,
freezeTableName: true,
}
);
return PackingConfigurations;
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const photos = {
const photo = {
tradeItemId: {
type: Sequelize.STRING,
primaryKey: true,
@ -18,9 +18,12 @@ const photos = {
};
export default (sequelize) => {
const Photos = sequelize.define('tradeItem_photos', photos, {
return sequelize.define(
'tradeItemPhoto',
photo,
{
timestamps: false,
freezeTableName: true,
});
return Photos;
}
);
};

View File

@ -16,9 +16,12 @@ const seasonalPeriod = {
};
export default (sequelize) => {
const SeasonalPeriod = sequelize.define('tradeItem_seasonalPeriod', seasonalPeriod, {
return sequelize.define(
'tradeItemSeasonalPeriod',
seasonalPeriod,
{
timestamps: false,
freezeTableName: true,
});
return SeasonalPeriod;
}
);
};

View File

@ -48,9 +48,12 @@ const tradeItem = {
};
export default (sequelize) => {
const TradeItem = sequelize.define('tradeItem', tradeItem, {
return sequelize.define(
'tradeItem',
tradeItem,
{
timestamps: false,
freezeTableName: true,
});
return TradeItem;
}
);
};

View File

@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
const warehouses = {
const warehouse = {
warehouseId: {
type: Sequelize.STRING,
primaryKey: true,
@ -48,12 +48,11 @@ const warehouses = {
};
export default (sequelize) => {
const Warehouses = sequelize.define(
'organization_warehouses',
warehouses, {
return sequelize.define(
'warehouse',
warehouse, {
timestamps: false,
freezeTableName: true,
}
);
return Warehouses;
};

View File

@ -16,7 +16,7 @@ const env = process.env;
export async function requestToken(isForce = false) {
let spinner = ora(`Requesting new token...`).start();
try {
const clientConfigData = await models.clientConfig.findOne();
const clientConfigData = await models.config.findOne();
let tokenExpiration, token;
if (clientConfigData) {
@ -62,7 +62,7 @@ export async function requestToken(isForce = false) {
* @returns {string}
*/
export async function getCurrentToken() {
return (await models.clientConfig.findOne()).currentToken;
return (await models.config.findOne()).currentToken;
}
/**
@ -81,7 +81,7 @@ export async function checkConfig() {
throw new Error(`You haven't provided the ${reqEnvVar} environment variable`);
}
}
const clientConfigData = await models.clientConfig.findOne();
const clientConfigData = await models.config.findOne();
if (!clientConfigData)
await updateClientConfig(env.CLIENT_ID, env.CLIENT_SECRET);
@ -94,7 +94,7 @@ export async function checkConfig() {
* @returns {string}
*/
export async function getCurrentTokenExpiration() {
return (await models.clientConfig.findOne()).tokenExpiration;
return (await models.config.findOne()).tokenExpiration;
}
/**
@ -105,7 +105,7 @@ export async function getCurrentTokenExpiration() {
export async function updateClientConfig(clientConfig) {
try {
if (!JSON.parse(process.env.USE_SECRETS_DB)) clientId = clientSecret = null
await models.clientConfig.upsert({
await models.config.upsert({
id: 1,
...clientConfig,
});
@ -267,7 +267,7 @@ export async function syncSupplyLines() {
for (let supplyLine of supplyLines) {
// Check if the warehouse exists, and if it doesn't, create it
let warehouse = await models.warehouses.findOne({
let warehouse = await models.warehouse.findOne({
where: { warehouseId: supplyLine.warehouseId }
});
if (!warehouse) {
@ -310,7 +310,7 @@ export async function syncSupplyLines() {
});
for (let volumePrice of supplyLine.volumePrices)
await models.volumePrices.upsert({
await models.volumePrice.upsert({
supplyLineId: supplyLine.supplyLineId,
...volumePrice,
});
@ -471,16 +471,16 @@ export async function insertItem(tradeItem) {
/**
* Insert clock presales supply in the database.
*
* @param {Array} clockPresalesSupply
* @param {Array} clockPresaleSupply
*/
export async function insertClockPresalesSupply(clockPresalesSupply) {
export async function insertClockPresalesSupply(clockPresaleSupply) {
const tx = await models.sequelize.transaction();
try {
await models.clockPresalesSupply.upsert({
...clockPresalesSupply,
pricePerPiece_currency: clockPresalesSupply.pricePerPiece.currency,
pricePerPiece_value: clockPresalesSupply.pricePerPiece.value,
organizationId: clockPresalesSupply.supplierOrganizationId,
await models.clockPresaleSupply.upsert({
...clockPresaleSupply,
pricePerPiece_currency: clockPresaleSupply.pricePerPiece.currency,
pricePerPiece_value: clockPresaleSupply.pricePerPiece.value,
organizationId: clockPresaleSupply.supplierOrganizationId,
});
await tx.commit();
} catch (err) {
@ -497,7 +497,7 @@ export async function insertClockPresalesSupply(clockPresalesSupply) {
export async function insertWarehouse(warehouse) {
const tx = await models.sequelize.transaction();
try {
await models.warehouses.upsert({
await models.warehouse.upsert({
...warehouse,
location_gln: warehouse.location.gln,
location_address_addressLine: warehouse.location.address.addressLine,