apply linter and add useful comments to the util.js functions

This commit is contained in:
Pau 2023-01-09 13:35:05 +01:00
parent f79662beb6
commit 82b6fba8f8
12 changed files with 242 additions and 229 deletions

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript"
]
}

View File

@ -6,9 +6,7 @@ dotenv.config();
import models from './models/index.js';
let AccessToken = await getClientToken(models);
let tokenExpirationDate = AccessToken[1];
let tokenExpirationDate = await getClientToken(models);
try {
setInterval(async () => {
@ -17,8 +15,7 @@ try {
if (moment().isAfter(tokenExpirationDate)) {
console.log('Token expired, getting a new one...');
AccessToken = await getClientToken(models);
tokenExpirationDate = AccessToken[1];
tokenExpirationDate = await getClientToken(models);
}
}, process.env.STATUS == 'development' ? 2500 : 5000);

View File

@ -1,31 +1,31 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const additionalPricePerPiece = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.INTEGER,
},
currency: {
type: Sequelize.STRING,
defaultValue: "EUR",
},
value: {
type: Sequelize.DECIMAL(10, 2),
},
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.INTEGER,
},
currency: {
type: Sequelize.STRING,
defaultValue: 'EUR',
},
value: {
type: Sequelize.DECIMAL(10, 2),
},
};
export default (sequelize) => {
const AdditionalPricePerPiece = sequelize.define(
"FDadditionalPricePerPiece",
additionalPricePerPiece,
{
timestamps: false,
freezeTableName: true,
}
);
return AdditionalPricePerPiece;
const AdditionalPricePerPiece = sequelize.define(
'FDadditionalPricePerPiece',
additionalPricePerPiece,
{
timestamps: false,
freezeTableName: true,
}
);
return AdditionalPricePerPiece;
};

View File

@ -1,18 +1,18 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const botanicalNames = {
tradeItemFk: {
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {
const BotanicalNames = sequelize.define("FDbotanicalNames", botanicalNames, {
timestamps: false,
freezeTableName: true,
});
return BotanicalNames;
const BotanicalNames = sequelize.define('FDbotanicalNames', botanicalNames, {
timestamps: false,
freezeTableName: true,
});
return BotanicalNames;
};

View File

@ -1,25 +1,25 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const characteristics = {
tradeItemFk: {
type: Sequelize.INTEGER,
},
vnbCode: {
type: Sequelize.STRING,
},
vnbValueCode: {
type: Sequelize.STRING,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
vnbCode: {
type: Sequelize.STRING,
},
vnbValueCode: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {
const Characteristics = sequelize.define(
"FDcharacteristics",
characteristics,
{
timestamps: false,
freezeTableName: true,
}
);
return Characteristics;
const Characteristics = sequelize.define(
'FDcharacteristics',
characteristics,
{
timestamps: false,
freezeTableName: true,
}
);
return Characteristics;
};

View File

@ -1,33 +1,33 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const clientConfig = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
clientId: {
type: Sequelize.STRING,
},
clientSecret: {
type: Sequelize.STRING,
},
currentToken: {
type: Sequelize.STRING,
},
tokenExpiration: {
type: Sequelize.STRING,
},
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
clientId: {
type: Sequelize.STRING,
},
clientSecret: {
type: Sequelize.STRING,
},
currentToken: {
type: Sequelize.STRING,
},
tokenExpiration: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {
const ClientConfig = sequelize.define(
"FDClientConfig",
clientConfig,
{
timestamps: false,
freezeTableName: true,
}
);
return ClientConfig;
const ClientConfig = sequelize.define(
'FDClientConfig',
clientConfig,
{
timestamps: false,
freezeTableName: true,
}
);
return ClientConfig;
};

View File

@ -1,22 +1,22 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const countryOfOriginIsoCodes = {
tradeItemFk: {
type: Sequelize.INTEGER,
},
isoCode: {
type: Sequelize.STRING,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
isoCode: {
type: Sequelize.STRING,
},
};
export default (sequelize) => {
const CountryOfOriginIsoCodes = sequelize.define(
"FDcountryOfOriginIsoCodes",
countryOfOriginIsoCodes,
{
timestamps: false,
freezeTableName: true,
}
);
return CountryOfOriginIsoCodes;
const CountryOfOriginIsoCodes = sequelize.define(
'FDcountryOfOriginIsoCodes',
countryOfOriginIsoCodes,
{
timestamps: false,
freezeTableName: true,
}
);
return CountryOfOriginIsoCodes;
};

View File

@ -1,26 +1,26 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const PackageModel = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.INTEGER,
},
vbnPackageCode: {
type: Sequelize.INTEGER,
},
customPackageId: {
type: Sequelize.UUID,
},
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
packingConfigurationsFk: {
type: Sequelize.INTEGER,
},
vbnPackageCode: {
type: Sequelize.INTEGER,
},
customPackageId: {
type: Sequelize.UUID,
},
};
export default (sequelize) => {
const Package = sequelize.define("FDpackage", PackageModel, {
timestamps: false,
freezeTableName: true,
});
return Package;
}
const Package = sequelize.define('FDpackage', PackageModel, {
timestamps: false,
freezeTableName: true,
});
return Package;
};

View File

@ -1,48 +1,48 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const packingConfigurations = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
piecesPerPackage: {
type: Sequelize.INTEGER,
},
bunchesPerPackage: {
type: Sequelize.INTEGER,
},
photoUrl: {
type: Sequelize.STRING,
},
packagesPerLayer: {
type: Sequelize.INTEGER,
},
layersPerLoadCarrier: {
type: Sequelize.INTEGER,
},
transportHeightInCm: {
type: Sequelize.INTEGER,
},
loadCarrierType: {
type: Sequelize.STRING,
},
isPrimary: {
type: Sequelize.BOOLEAN,
},
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
piecesPerPackage: {
type: Sequelize.INTEGER,
},
bunchesPerPackage: {
type: Sequelize.INTEGER,
},
photoUrl: {
type: Sequelize.STRING,
},
packagesPerLayer: {
type: Sequelize.INTEGER,
},
layersPerLoadCarrier: {
type: Sequelize.INTEGER,
},
transportHeightInCm: {
type: Sequelize.INTEGER,
},
loadCarrierType: {
type: Sequelize.STRING,
},
isPrimary: {
type: Sequelize.BOOLEAN,
},
};
export default (sequelize) => {
const PackingConfigurations = sequelize.define(
"FDpackingConfigurations",
packingConfigurations,
{
timestamps: false,
freezeTableName: true,
}
);
return PackingConfigurations;
const PackingConfigurations = sequelize.define(
'FDpackingConfigurations',
packingConfigurations,
{
timestamps: false,
freezeTableName: true,
}
);
return PackingConfigurations;
};

View File

@ -1,30 +1,30 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const photos = {
tradeItemFk: {
type: Sequelize.INTEGER,
},
photoId: {
type: Sequelize.UUID,
},
url: {
type: Sequelize.STRING,
},
type: {
type: Sequelize.STRING,
},
primary: {
type: Sequelize.BOOLEAN,
},
seasonalPeriodFk: {
type: Sequelize.INTEGER,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
photoId: {
type: Sequelize.UUID,
},
url: {
type: Sequelize.STRING,
},
type: {
type: Sequelize.STRING,
},
primary: {
type: Sequelize.BOOLEAN,
},
seasonalPeriodFk: {
type: Sequelize.INTEGER,
},
};
export default (sequelize) => {
const Photos = sequelize.define("FDphotos", photos, {
timestamps: false,
freezeTableName: true,
});
return Photos;
}
const Photos = sequelize.define('FDphotos', photos, {
timestamps: false,
freezeTableName: true,
});
return Photos;
};

View File

@ -1,26 +1,26 @@
import { Sequelize } from "sequelize";
import { Sequelize } from 'sequelize';
const seasonalPeriod = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
startWeek: {
type: Sequelize.INTEGER,
},
endWeek: {
type: Sequelize.INTEGER,
},
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tradeItemFk: {
type: Sequelize.INTEGER,
},
startWeek: {
type: Sequelize.INTEGER,
},
endWeek: {
type: Sequelize.INTEGER,
},
};
export default (sequelize) => {
const SeasonalPeriod = sequelize.define("FDseasonalPeriod", seasonalPeriod, {
timestamps: false,
freezeTableName: true,
});
return SeasonalPeriod;
const SeasonalPeriod = sequelize.define('FDseasonalPeriod', seasonalPeriod, {
timestamps: false,
freezeTableName: true,
});
return SeasonalPeriod;
};

View File

@ -1,19 +1,23 @@
import moment from 'moment';
import fetch from 'node-fetch';
/**
* The Endpoint where the Access Token is requested
*/
const _accessTokenEndpoint = 'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token';
const _accessTokenEndpoint =
'https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token';
/**
* Gets the Access Token from the client config table
*
* @param {sequelize.models} models
* @returns {Date} tokenExpirationDate formated as YYYY-MM-DD HH:mm:ss
*/
async function getClientToken(models) {
const clientConfigData = await models.clientConfig.findAll();
const now = moment().format('YYYY-MM-DD HH:mm:ss');
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
if (
clientConfigData[0].tokenExpiration == null ||
moment(now).isAfter(tokenExpirationDate)
) {
if (clientConfigData[0].tokenExpiration == null || moment(now).isAfter(tokenExpirationDate)) {
let clientId = clientConfigData[0].clientId;
let clientSecret = clientConfigData[0].clientSecret;
@ -50,20 +54,24 @@ async function getClientToken(models) {
tokenExpirationDate
);
return [accessToken, tokenExpirationDate];
return tokenExpirationDate;
} else {
console.log('Using the current token...');
return [clientConfigData[0].currentToken, tokenExpirationDate];
return tokenExpirationDate;
}
}
async function updateClientConfig(
models,
clientId,
clientSecret,
accessToken,
tokenExpirationDate
) {
/**
* Updates the Access Token in the client config table
*
* @param {sequelize.models} models
* @param {String} clientId
* @param {String} clientSecret
* @param {String} accessToken
* @param {String} tokenExpirationDate
*/
async function updateClientConfig(models, clientId, clientSecret, accessToken, tokenExpirationDate) {
try {
console.log('Updating the client config with the new token...');
await models.clientConfig.update(
@ -87,4 +95,4 @@ async function updateClientConfig(
}
}
export { getClientToken };
export { getClientToken, updateClientConfig };