From 6a3f5113f1d014d3f5bda5b0ac58d325ce5f1326 Mon Sep 17 00:00:00 2001 From: Lucas Tonetto Firmo Date: Thu, 29 Apr 2021 17:35:20 -0300 Subject: [PATCH] feat: :sparkles: Insercao da campanha Salvamento dos arquivos e listagem dos CSVs com base na campanha informada --- dist/app.js | 2 +- dist/models/DAO/FileDAO.js | 4 +- .../cloud/FirestoreConnectionSingleton.js | 2 +- .../cloud/StorageConnectionSingleton.js | 4 +- dist/routes/build.js | 9 ++++- dist/routes/csv.js | 28 ++++++++++++-- src/ts/app.ts | 2 +- src/ts/models/DAO/FileDAO.ts | 4 +- .../cloud/FirestoreConnectionSingleton.ts | 2 +- .../cloud/StorageConnectionSingleton.ts | 4 +- src/ts/routes/build.ts | 9 ++++- src/ts/routes/csv.ts | 37 +++++++++++++++++-- 12 files changed, 87 insertions(+), 20 deletions(-) diff --git a/dist/app.js b/dist/app.js index 6f7425d2..25c7ea32 100644 --- a/dist/app.js +++ b/dist/app.js @@ -38,7 +38,7 @@ const fileUpload = require('express-fileupload'); const routes_1 = require('./routes/routes'); const dotenv_1 = require('dotenv'); const AuthDAO_1 = require('./models/DAO/AuthDAO'); -dotenv_1.config({ path: __dirname + '/../../.env' }); +dotenv_1.config({ path: __dirname + '/../.env' }); const app = express(); app.use( fileUpload({ diff --git a/dist/models/DAO/FileDAO.js b/dist/models/DAO/FileDAO.js index a9815d6b..7fb19855 100644 --- a/dist/models/DAO/FileDAO.js +++ b/dist/models/DAO/FileDAO.js @@ -18,8 +18,8 @@ class FileDAO { getFromStore(filePath) { return this._fileStore.getFile(filePath); } - getAllFilesFromStore(agency) { - return this._fileStore.getAllFiles(agency); + getAllFilesFromStore(path) { + return this._fileStore.getAllFiles(path); } } exports.FileDAO = FileDAO; diff --git a/dist/models/cloud/FirestoreConnectionSingleton.js b/dist/models/cloud/FirestoreConnectionSingleton.js index 5494ac87..4f510e2b 100644 --- a/dist/models/cloud/FirestoreConnectionSingleton.js +++ b/dist/models/cloud/FirestoreConnectionSingleton.js @@ -7,7 +7,7 @@ class FirestoreConnectionSingleton extends ObjectStore_1.ObjectStore { constructor() { super(); if (process.env.DEVELOPMENT) { - const credentials = require('../../../../gcp_key.json'); + const credentials = require('../../../gcp_key.json'); this._db = new firestore_1.Firestore({ projectId: 'adinfo', credentials }); } else { this._db = new firestore_1.Firestore(); diff --git a/dist/models/cloud/StorageConnectionSingleton.js b/dist/models/cloud/StorageConnectionSingleton.js index 55164422..415304b2 100644 --- a/dist/models/cloud/StorageConnectionSingleton.js +++ b/dist/models/cloud/StorageConnectionSingleton.js @@ -8,7 +8,7 @@ class StorageConnectionSingleton extends FileStore_1.FileStore { super(); this._bucket = `adinfo-dp6-files`; if (process.env.DEVELOPMENT) { - const credentials = require('../../../../gcp_key.json'); + const credentials = require('../../../gcp_key.json'); this._db = new storage_1.Storage({ projectId: 'adinfo', credentials }); } else { this._db = new storage_1.Storage(); @@ -30,7 +30,7 @@ class StorageConnectionSingleton extends FileStore_1.FileStore { return destinationPath.download(); } getAllFiles(folder) { - return this._db.bucket(this._bucket).getFiles({ prefix: `${folder}/` }); + return this._db.bucket(this._bucket).getFiles({ prefix: folder }); } } exports.StorageConnectionSingleton = StorageConnectionSingleton; diff --git a/dist/routes/build.js b/dist/routes/build.js index 47c8fe2e..6bb33e3e 100644 --- a/dist/routes/build.js +++ b/dist/routes/build.js @@ -11,14 +11,21 @@ const build = (app) => { const media = req.params.media; const company = req.company; const agency = req.agency; + const campaign = req.headers.campaign; if (!req.files.data) { res.status(400).send({ message: 'Nenhum arquivo foi enviado!', }); return; + } else if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); } const fileContent = req.files.data.data; - const filePath = `${agency}/${DateUtils_1.DateUtils.generateDateString()}.csv`; + const filePath = agency + ? `${company}/${agency}/${campaign}/${DateUtils_1.DateUtils.generateDateString()}.csv` + : `${company}/${campaign}/${DateUtils_1.DateUtils.generateDateString()}.csv`; let companyConfig; const configDAO = new ConfigDAO_1.ConfigDAO(company); configDAO diff --git a/dist/routes/csv.js b/dist/routes/csv.js index 0d47a9fc..d5994fac 100644 --- a/dist/routes/csv.js +++ b/dist/routes/csv.js @@ -4,9 +4,18 @@ const FileDAO_1 = require('../models/DAO/FileDAO'); const DateUtils_1 = require('../utils/DateUtils'); const csv = (app) => { app.post('/csv', (req, res) => { + const campaign = req.headers.campaign; const content = req.files.data.data; const agency = req.agency; - const filePath = `${agency}/${DateUtils_1.DateUtils.generateDateString()}.csv`; + const company = req.company; + if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); + } + const filePath = agency + ? `${company}/${agency}/${campaign}/${DateUtils_1.DateUtils.generateDateString()}.csv` + : `${company}/${campaign}/${DateUtils_1.DateUtils.generateDateString()}.csv`; const fileDAO = new FileDAO_1.FileDAO(); fileDAO.file = content; fileDAO @@ -21,13 +30,21 @@ const csv = (app) => { app.get('/csv', (req, res) => { const fileName = req.headers.file; const agency = req.agency; + const campaign = req.headers.campaign; + const company = req.company; if (!fileName) { res.status(400).send({ message: 'Nenhum arquivo foi enviado!', }); return; + } else if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); } - const filePath = `${agency}/${fileName}.csv`; + const filePath = agency + ? `${company}/${agency}/${campaign}/${fileName}.csv` + : `${company}/${campaign}/${fileName}.csv`; const fileDAO = new FileDAO_1.FileDAO(); fileDAO .getFromStore(filePath) @@ -40,9 +57,14 @@ const csv = (app) => { }); app.get('/csv/list', (req, res) => { const agency = req.agency; + const company = req.company; + const campaign = req.headers.campaign; const fileDAO = new FileDAO_1.FileDAO(); + let filePath = `${company}/`; + if (agency) filePath += `${agency}/`; + if (campaign) filePath += `${campaign}/`; fileDAO - .getAllFilesFromStore(agency) + .getAllFilesFromStore(filePath) .then((data) => { const files = data[0].filter((file) => /\.csv$/.test(file.name)).map((file) => file.name); res.status(200).send(files); diff --git a/src/ts/app.ts b/src/ts/app.ts index 4e646ddf..17c1343d 100644 --- a/src/ts/app.ts +++ b/src/ts/app.ts @@ -7,7 +7,7 @@ import { config } from 'dotenv'; import { Auth } from './models/Auth'; import { AuthDAO } from './models/DAO/AuthDAO'; -config({ path: __dirname + '/../../.env' }); +config({ path: __dirname + '/../.env' }); const app = express(); diff --git a/src/ts/models/DAO/FileDAO.ts b/src/ts/models/DAO/FileDAO.ts index 72827f3d..8a2f2a7b 100644 --- a/src/ts/models/DAO/FileDAO.ts +++ b/src/ts/models/DAO/FileDAO.ts @@ -37,7 +37,7 @@ export class FileDAO { * Pegar todos os arquivos de uma agencia * @param agency nome da agencia */ - public getAllFilesFromStore(agency: string): Promise { - return this._fileStore.getAllFiles(agency); + public getAllFilesFromStore(path: string): Promise { + return this._fileStore.getAllFiles(path); } } diff --git a/src/ts/models/cloud/FirestoreConnectionSingleton.ts b/src/ts/models/cloud/FirestoreConnectionSingleton.ts index 292b2220..10b4eb4b 100644 --- a/src/ts/models/cloud/FirestoreConnectionSingleton.ts +++ b/src/ts/models/cloud/FirestoreConnectionSingleton.ts @@ -9,7 +9,7 @@ export class FirestoreConnectionSingleton extends ObjectStore { super(); if (process.env.DEVELOPMENT) { /* eslint-disable @typescript-eslint/no-var-requires */ - const credentials = require('../../../../gcp_key.json'); + const credentials = require('../../../gcp_key.json'); /* eslint-enable @typescript-eslint/no-var-requires */ this._db = new Firestore({ projectId: 'adinfo', credentials }); } else { diff --git a/src/ts/models/cloud/StorageConnectionSingleton.ts b/src/ts/models/cloud/StorageConnectionSingleton.ts index 3fad91a5..1530d11f 100644 --- a/src/ts/models/cloud/StorageConnectionSingleton.ts +++ b/src/ts/models/cloud/StorageConnectionSingleton.ts @@ -11,7 +11,7 @@ export class StorageConnectionSingleton extends FileStore { super(); if (process.env.DEVELOPMENT) { /* eslint-disable @typescript-eslint/no-var-requires */ - const credentials = require('../../../../gcp_key.json'); + const credentials = require('../../../gcp_key.json'); /* eslint-enable @typescript-eslint/no-var-requires */ this._db = new Storage({ projectId: 'adinfo', credentials }); } else { @@ -54,6 +54,6 @@ export class StorageConnectionSingleton extends FileStore { * @param folder pasta dos arquivos */ public getAllFiles(folder: string): Promise { - return this._db.bucket(this._bucket).getFiles({ prefix: `${folder}/` }); + return this._db.bucket(this._bucket).getFiles({ prefix: folder }); } } diff --git a/src/ts/routes/build.ts b/src/ts/routes/build.ts index 7cabca34..d06e2faf 100644 --- a/src/ts/routes/build.ts +++ b/src/ts/routes/build.ts @@ -11,15 +11,22 @@ const build = (app: { [key: string]: any }): void => { const media = req.params.media; const company = req.company; const agency = req.agency; + const campaign = req.headers.campaign; if (!req.files.data) { res.status(400).send({ message: 'Nenhum arquivo foi enviado!', }); return; + } else if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); } const fileContent = req.files.data.data; - const filePath = `${agency}/${DateUtils.generateDateString()}.csv`; + const filePath = agency + ? `${company}/${agency}/${campaign}/${DateUtils.generateDateString()}.csv` + : `${company}/${campaign}/${DateUtils.generateDateString()}.csv`; let companyConfig: Config; diff --git a/src/ts/routes/csv.ts b/src/ts/routes/csv.ts index 8166af26..307ef35a 100644 --- a/src/ts/routes/csv.ts +++ b/src/ts/routes/csv.ts @@ -3,9 +3,21 @@ import { DateUtils } from '../utils/DateUtils'; const csv = (app: { [key: string]: any }): void => { app.post('/csv', (req: { [key: string]: any }, res: { [key: string]: any }) => { + const campaign = req.headers.campaign; const content = req.files.data.data; const agency = req.agency; - const filePath = `${agency}/${DateUtils.generateDateString()}.csv`; + const company = req.company; + + if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); + } + + const filePath = agency + ? `${company}/${agency}/${campaign}/${DateUtils.generateDateString()}.csv` + : `${company}/${campaign}/${DateUtils.generateDateString()}.csv`; + const fileDAO = new FileDAO(); fileDAO.file = content; fileDAO @@ -21,13 +33,24 @@ const csv = (app: { [key: string]: any }): void => { app.get('/csv', (req: { [key: string]: any }, res: { [key: string]: any }) => { const fileName = req.headers.file; const agency = req.agency; + const campaign = req.headers.campaign; + const company = req.company; + if (!fileName) { res.status(400).send({ message: 'Nenhum arquivo foi enviado!', }); return; + } else if (!campaign) { + res.status(400).send({ + message: 'Nenhuma campanha foi informada!', + }); } - const filePath = `${agency}/${fileName}.csv`; + + const filePath = agency + ? `${company}/${agency}/${campaign}/${fileName}.csv` + : `${company}/${campaign}/${fileName}.csv`; + const fileDAO = new FileDAO(); fileDAO .getFromStore(filePath) @@ -42,9 +65,17 @@ const csv = (app: { [key: string]: any }): void => { app.get('/csv/list', (req: { [key: string]: any }, res: { [key: string]: any }) => { const agency = req.agency; + const company = req.company; + const campaign = req.headers.campaign; const fileDAO = new FileDAO(); + + let filePath = `${company}/`; + + if (agency) filePath += `${agency}/`; + if (campaign) filePath += `${campaign}/`; + fileDAO - .getAllFilesFromStore(agency) + .getAllFilesFromStore(filePath) .then((data) => { const files = data[0].filter((file) => /\.csv$/.test(file.name)).map((file) => file.name); res.status(200).send(files);