Skip to content

Commit

Permalink
feat: ✨ Insercao da campanha
Browse files Browse the repository at this point in the history
Salvamento dos arquivos e listagem dos CSVs com base na campanha informada
  • Loading branch information
LucasTonetto committed May 5, 2021
1 parent 37e66fc commit 6a3f511
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions dist/models/DAO/FileDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion dist/models/cloud/FirestoreConnectionSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions dist/models/cloud/StorageConnectionSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
9 changes: 8 additions & 1 deletion dist/routes/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions dist/routes/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/ts/models/DAO/FileDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class FileDAO {
* Pegar todos os arquivos de uma agencia
* @param agency nome da agencia
*/
public getAllFilesFromStore(agency: string): Promise<File[][]> {
return this._fileStore.getAllFiles(agency);
public getAllFilesFromStore(path: string): Promise<File[][]> {
return this._fileStore.getAllFiles(path);
}
}
2 changes: 1 addition & 1 deletion src/ts/models/cloud/FirestoreConnectionSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/ts/models/cloud/StorageConnectionSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,6 +54,6 @@ export class StorageConnectionSingleton extends FileStore {
* @param folder pasta dos arquivos
*/
public getAllFiles(folder: string): Promise<File[][]> {
return this._db.bucket(this._bucket).getFiles({ prefix: `${folder}/` });
return this._db.bucket(this._bucket).getFiles({ prefix: folder });
}
}
9 changes: 8 additions & 1 deletion src/ts/routes/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
37 changes: 34 additions & 3 deletions src/ts/routes/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 6a3f511

Please sign in to comment.