first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
const Agenda = require("agenda");
|
||||
const agenda = new Agenda({ db: { address: "mongodb://db:27017/agendaDb" } });
|
||||
const {Agent} = require('./agent');
|
||||
const agent = new Agent();
|
||||
|
||||
// Define a job
|
||||
agenda.define('followSale', async (job, done) => {
|
||||
const { saleId } = job.attrs.data;
|
||||
agent.followSale(saleId)
|
||||
.then(data => {;
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
agenda.define('prepareSale', async (job, done) => {
|
||||
const { saleId } = job.attrs.data;
|
||||
agent.prepareSale(saleId)
|
||||
.then(data => {;
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
module.exports = agenda;
|
||||
@@ -0,0 +1,89 @@
|
||||
const fetch = require('node-fetch');
|
||||
const { LotDb } = require("./lotDb");
|
||||
const lotDb = new LotDb();
|
||||
const { SaleDb } = require("./saleDb");
|
||||
const saleDb = new SaleDb();
|
||||
const moment = require('moment-timezone');
|
||||
|
||||
const Agent = class
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.ApiAgentURL = "http://host.docker.internal:3020/api";
|
||||
}
|
||||
|
||||
async getSaleInfos(url)
|
||||
{
|
||||
return new Promise((resolve, reject) => {
|
||||
url = encodeURIComponent(url);
|
||||
fetch(this.ApiAgentURL+'/sale/getSaleInfos/'+url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
resolve(data);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async prepareSale(id)
|
||||
{
|
||||
return new Promise(async (resolve, reject) => {
|
||||
|
||||
let Sale = await saleDb.get(id);
|
||||
|
||||
const DateSale = moment.tz(Sale.date, "Europe/Paris");
|
||||
const NowParis = moment.tz(new Date(),"Europe/Paris")
|
||||
|
||||
if (NowParis.isBefore(DateSale)){
|
||||
|
||||
let url = Sale.url
|
||||
url = encodeURIComponent(url);
|
||||
|
||||
fetch(this.ApiAgentURL+'/sale/getLotList/'+url)
|
||||
.then(response => response.json())
|
||||
.then( async data => {
|
||||
|
||||
for (let lot of data) {
|
||||
lot.sale_id = Sale._id
|
||||
await lotDb.post(lot);
|
||||
}
|
||||
resolve(data);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
}else{
|
||||
console.log("Sale started or finished");
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async followSale(id)
|
||||
{
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let Sale = await saleDb.get(id);
|
||||
|
||||
let url = Sale.url
|
||||
url = encodeURIComponent(url);
|
||||
|
||||
fetch(this.ApiAgentURL+'/sale/followSale/'+url)
|
||||
.then(response => response.json())
|
||||
.then(async data => {
|
||||
|
||||
// set the Sale status to following
|
||||
Sale.status = "following";
|
||||
Sale = await saleDb.put(id, Sale);
|
||||
resolve(data);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {Agent};
|
||||
@@ -0,0 +1,18 @@
|
||||
const MongoClient = require("mongodb").MongoClient;
|
||||
const connectionString = "mongodb://db:27017";
|
||||
const client = new MongoClient(connectionString);
|
||||
|
||||
let db;
|
||||
|
||||
const connectDb = async () => {
|
||||
if (db) return db;
|
||||
try {
|
||||
const conn = await client.connect();
|
||||
db = conn.db("jucundus");
|
||||
return db;
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = connectDb;
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
const connectDb = require("./db");
|
||||
|
||||
const save = async (newDocument) => {
|
||||
const db = await connectDb();
|
||||
if (!db) {
|
||||
throw new Error('Database not connected');
|
||||
}
|
||||
|
||||
const collection = db.collection("Favorites");
|
||||
let result = await collection.insertOne(newDocument);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const getAll = async () => {
|
||||
const db = await connectDb();
|
||||
if (!db) {
|
||||
throw new Error('Database not connected');
|
||||
}
|
||||
const collection = db.collection("Favorites");
|
||||
let result = await collection.find({}).toArray();
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
module.exports = { save, getAll };
|
||||
@@ -0,0 +1,75 @@
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
const connectDb = require("./db");
|
||||
|
||||
const LotDb = class
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.getCollection();
|
||||
}
|
||||
|
||||
async getCollection()
|
||||
{
|
||||
const db = await connectDb();
|
||||
if (!db) {
|
||||
throw new Error('Database not connected');
|
||||
}
|
||||
this.collection = db.collection("Lots");
|
||||
}
|
||||
|
||||
// CRUD
|
||||
async get(id)
|
||||
{
|
||||
let result = await this.collection.findOne({_id: new ObjectId(id)});
|
||||
return result;
|
||||
}
|
||||
|
||||
async post(newDocument)
|
||||
{
|
||||
let result = await this.collection.insertOne(newDocument);
|
||||
return result;
|
||||
}
|
||||
|
||||
async put(id, data)
|
||||
{
|
||||
let result = await this.collection.updateOne({_id: new ObjectId(id)}, {$set: data});
|
||||
return result;
|
||||
}
|
||||
|
||||
async remove(id)
|
||||
{
|
||||
let result = await this.collection.deleteOne({_id: new ObjectId(id)});
|
||||
return result;
|
||||
}
|
||||
|
||||
// Fucntions
|
||||
|
||||
async getAll()
|
||||
{
|
||||
let result = await this.collection.find({}).toArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
async getBySaleId(idSalePlatform, platformName)
|
||||
{
|
||||
console.log(platformName);
|
||||
let result = await this.collection.find({sale_id: new ObjectId(idSalePlatform), platform: platformName});
|
||||
return result.toArray();
|
||||
}
|
||||
|
||||
async getByIDPlatform(idLotPlatform, platformName)
|
||||
{
|
||||
let result = await this.collection.findOne({idPlatform: String(idLotPlatform), platform: platformName});
|
||||
return result;
|
||||
}
|
||||
|
||||
async deleteAllLotBySaleId(sale_id){
|
||||
let result = await this.collection.deleteMany({sale_id: new ObjectId(sale_id)});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {LotDb};
|
||||
@@ -0,0 +1,67 @@
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
const connectDb = require("./db");
|
||||
|
||||
const SaleDb = class
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.getCollection();
|
||||
}
|
||||
|
||||
async getCollection()
|
||||
{
|
||||
const db = await connectDb();
|
||||
if (!db) {
|
||||
throw new Error('Database not connected');
|
||||
}
|
||||
this.collection = db.collection("Sales");
|
||||
}
|
||||
|
||||
// CRUD
|
||||
async get(id)
|
||||
{
|
||||
let result = await this.collection.findOne({_id: new ObjectId(id)});
|
||||
return result;
|
||||
}
|
||||
|
||||
async post(newDocument)
|
||||
{
|
||||
let result = await this.collection.insertOne(newDocument);
|
||||
return result;
|
||||
}
|
||||
|
||||
async put(id, data)
|
||||
{
|
||||
let result = await this.collection.updateOne({_id: new ObjectId(id)}, {$set: data});
|
||||
return result;
|
||||
}
|
||||
|
||||
async remove(id)
|
||||
{
|
||||
let result = await this.collection.deleteOne({_id: new ObjectId(id)});
|
||||
return result;
|
||||
}
|
||||
|
||||
// Fucntions
|
||||
|
||||
async getAll()
|
||||
{
|
||||
let result = await this.collection.find({}).toArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
async getByUrl(url)
|
||||
{
|
||||
let result = await this.collection.findOne({url: url});
|
||||
return result;
|
||||
}
|
||||
|
||||
async getByIDPlatform(idSalePlatform, platformName)
|
||||
{
|
||||
let result = await this.collection.findOne({idPlatform: String(idSalePlatform), platform: String(platformName)});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { SaleDb };
|
||||
Reference in New Issue
Block a user