remove testing code, updated readme, updated formatter
This commit is contained in:
parent
fcd0b5f1ec
commit
7be0fdfe3c
47
README.md
47
README.md
|
@ -14,7 +14,52 @@ The Floriday service project should perform the following tasks:
|
||||||
|
|
||||||
## Guidelines
|
## Guidelines
|
||||||
|
|
||||||
- In case a new model is created, it must be created in the `models` folder and follow the same structure as the other models.
|
- In case a new model is created, it should follow the following structure:
|
||||||
|
|
||||||
|
- /models
|
||||||
|
- index.js
|
||||||
|
- foo.js
|
||||||
|
|
||||||
|
### Foo.js
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import { Sequelize } from "sequelize";
|
||||||
|
|
||||||
|
const bar = {
|
||||||
|
barCharacteristicOne: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
},
|
||||||
|
barCharacteristicTwo: {
|
||||||
|
type: Sequelize.STRING,
|
||||||
|
},
|
||||||
|
barCharacteristicThree: {
|
||||||
|
type: Sequelize.STRING,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default (sequelize) => {
|
||||||
|
const Foo = sequelize.define(
|
||||||
|
"FDfoo",
|
||||||
|
bar,
|
||||||
|
{
|
||||||
|
timestamps: false,
|
||||||
|
freezeTableName: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return Foo;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Index.js
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import foo from "./foo";
|
||||||
|
|
||||||
|
let models = {
|
||||||
|
bar: foo(sequelize),
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
To install dependencies:
|
To install dependencies:
|
||||||
|
|
||||||
|
|
227
index.js
227
index.js
|
@ -2,7 +2,7 @@ import fetch from "node-fetch";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
|
|
||||||
const _accessTokenEndpoint =
|
const _accessTokenEndpoint =
|
||||||
"https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token";
|
"https://idm.staging.floriday.io/oauth2/ausmw6b47z1BnlHkw0h7/v1/token";
|
||||||
|
|
||||||
import models from "./models/index.js";
|
import models from "./models/index.js";
|
||||||
|
|
||||||
|
@ -12,178 +12,97 @@ let tokenValue = AccessToken[0];
|
||||||
let tokenExpirationDate = AccessToken[1];
|
let tokenExpirationDate = AccessToken[1];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Every 30 sec query the database
|
// Every 30 sec query the database
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
console.log("Querying the API to check for new data...");
|
console.log("Querying the API to check for new data...");
|
||||||
console.log("Current token expiration date: ", tokenExpirationDate);
|
console.log("Current token expiration date: ", tokenExpirationDate);
|
||||||
console.log("Now is: ", moment().format("YYYY-MM-DD HH:mm:ss"));
|
|
||||||
|
|
||||||
if(moment().isAfter(tokenExpirationDate)){
|
if (moment().isAfter(tokenExpirationDate)) {
|
||||||
console.log("Token expired, getting a new one...");
|
console.log("Token expired, getting a new one...");
|
||||||
AccessToken = await getClientToken();
|
AccessToken = await getClientToken();
|
||||||
tokenValue = AccessToken[0];
|
tokenValue = AccessToken[0];
|
||||||
tokenExpirationDate = AccessToken[1];
|
tokenExpirationDate = AccessToken[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = models.tradeItem.findAll({
|
}, 5000);
|
||||||
include: [
|
|
||||||
{
|
|
||||||
model: models.characteristics,
|
|
||||||
association: models.tradeItem.hasMany(models.characteristics, {
|
|
||||||
foreignKey: "tradeItemFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "characteristics",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: models.photos,
|
|
||||||
association: models.tradeItem.hasMany(models.photos, {
|
|
||||||
foreignKey: "tradeItemFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "photos",
|
|
||||||
include: [
|
|
||||||
{
|
|
||||||
model: models.seasonalPeriod,
|
|
||||||
association: models.photos.hasOne(models.seasonalPeriod, {
|
|
||||||
foreignKey: "id",
|
|
||||||
sourceKey: "seasonalPeriodFk",
|
|
||||||
}),
|
|
||||||
as: "seasonalPeriod",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: models.packagingConfigurations,
|
|
||||||
association: models.tradeItem.hasMany(models.packagingConfigurations, {
|
|
||||||
foreignKey: "tradeItemFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "packagingConfigurations",
|
|
||||||
include: [
|
|
||||||
{
|
|
||||||
model: models.package,
|
|
||||||
association: models.packagingConfigurations.hasOne(models.package, {
|
|
||||||
foreignKey: "packingConfigurationsFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "package",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: models.additionalPricePerPiece,
|
|
||||||
association: models.packagingConfigurations.hasOne(
|
|
||||||
models.additionalPricePerPiece,
|
|
||||||
{
|
|
||||||
foreignKey: "packingConfigurationsFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}
|
|
||||||
),
|
|
||||||
as: "additionalPricePerPiece",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: models.botanicalNames,
|
|
||||||
association: models.tradeItem.hasMany(models.botanicalNames, {
|
|
||||||
foreignKey: "tradeItemFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "botanicalNames",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: models.countryOfOriginIsoCodes,
|
|
||||||
association: models.tradeItem.hasMany(models.countryOfOriginIsoCodes, {
|
|
||||||
foreignKey: "tradeItemFk",
|
|
||||||
sourceKey: "id",
|
|
||||||
}),
|
|
||||||
as: "countryOfOriginIsoCodes",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await query;
|
|
||||||
|
|
||||||
console.log(JSON.stringify(result, null, 2));
|
|
||||||
}, 5000);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Unable to connect to the database:", error);
|
console.error("Unable to connect to the database:", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getClientToken() {
|
async function getClientToken() {
|
||||||
|
let clientConfigData = await models.clientConfig.findAll();
|
||||||
|
|
||||||
let clientConfigData = await models.clientConfig.findAll();
|
const now = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||||
|
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
||||||
|
|
||||||
const now = moment().format("YYYY-MM-DD HH:mm:ss");
|
if (
|
||||||
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
clientConfigData[0].tokenExpiration == null ||
|
||||||
|
moment(now).isAfter(tokenExpirationDate)
|
||||||
|
) {
|
||||||
|
let clientId = clientConfigData[0].clientId;
|
||||||
|
let clientSecret = clientConfigData[0].clientSecret;
|
||||||
|
|
||||||
if (
|
const tokenRequest = await fetch(_accessTokenEndpoint, {
|
||||||
clientConfigData[0].tokenExpiration == null ||
|
method: "POST",
|
||||||
moment(now).isAfter(tokenExpirationDate)
|
headers: {
|
||||||
) {
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
let clientId = clientConfigData[0].clientId;
|
},
|
||||||
let clientSecret = clientConfigData[0].clientSecret;
|
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read organization:read`,
|
||||||
|
});
|
||||||
|
|
||||||
const tokenRequest = await fetch(_accessTokenEndpoint, {
|
const tokenResponse = await tokenRequest.json();
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
|
||||||
},
|
|
||||||
body: `grant_type=client_credentials&client_id=${clientId}&client_secret=${clientSecret}&scope=role:app catalog:read`,
|
|
||||||
});
|
|
||||||
|
|
||||||
const tokenResponse = await tokenRequest.json();
|
const accessToken = tokenResponse.access_token;
|
||||||
|
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||||
|
|
||||||
const accessToken = tokenResponse.access_token;
|
let tokenExpirationDate = moment(now)
|
||||||
let now = moment().format("YYYY-MM-DD HH:mm:ss");
|
.add(tokenResponse.expires_in, "s")
|
||||||
|
.format("YYYY-MM-DD HH:mm:ss");
|
||||||
|
|
||||||
let tokenExpirationDate = moment(now)
|
updateClientConfig(
|
||||||
.add(tokenResponse.expires_in, "s")
|
clientId,
|
||||||
.format("YYYY-MM-DD HH:mm:ss");
|
clientSecret,
|
||||||
|
accessToken,
|
||||||
|
tokenExpirationDate
|
||||||
|
);
|
||||||
|
|
||||||
updateClientConfig(
|
return [accessToken, tokenExpirationDate];
|
||||||
clientId,
|
} else {
|
||||||
clientSecret,
|
console.log("Using the current token...");
|
||||||
accessToken,
|
return [clientConfigData[0].currentToken, tokenExpirationDate];
|
||||||
tokenExpirationDate
|
}
|
||||||
);
|
|
||||||
|
|
||||||
return [accessToken, tokenExpirationDate];
|
|
||||||
} else {
|
|
||||||
console.log("Using the current token...");
|
|
||||||
return [clientConfigData[0].currentToken, tokenExpirationDate];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateClientConfig(
|
async function updateClientConfig(
|
||||||
clientId,
|
clientId,
|
||||||
clientSecret,
|
clientSecret,
|
||||||
accessToken,
|
accessToken,
|
||||||
tokenExpirationDate
|
tokenExpirationDate
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await models.clientConfig.update(
|
await models.clientConfig.update(
|
||||||
{
|
{
|
||||||
clientId: clientId,
|
clientId: clientId,
|
||||||
clientSecret: clientSecret,
|
clientSecret: clientSecret,
|
||||||
currentToken: accessToken,
|
currentToken: accessToken,
|
||||||
tokenExpiration: tokenExpirationDate,
|
tokenExpiration: tokenExpirationDate,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
console.log("Client config updated, new Token set");
|
console.log("Client config updated, new Token set");
|
||||||
console.log("New token expiration date: ", tokenExpirationDate );
|
console.log("New token expiration date: ", tokenExpirationDate);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("There was a error while updating the client config");
|
console.log("There was a error while updating the client config");
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log = function () {
|
console.log = function () {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
args.unshift(new moment().format("HH:mm:ss") + " -");
|
args.unshift(new moment().format("HH:mm:ss") + " -");
|
||||||
console.info.apply(console, args);
|
console.info.apply(console, args);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue