Skip to content

Commit

Permalink
feat: 🎨 Rota Campaign, CampaignDAO e Campaign object
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Ueti committed Oct 7, 2021
1 parent ee584ed commit c9b4195
Show file tree
Hide file tree
Showing 8 changed files with 591 additions and 145 deletions.
41 changes: 41 additions & 0 deletions dist/models/Campaign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Campaign = void 0;
class Campaign {
constructor(name, company, agency, campaignId, activate = true, created) {
this._name = name;
this._company = company;
this._agency = agency;
this._campaignId = campaignId;
this._activate = activate;
this._created = created;
}
toJson() {
return {
company: this._company,
agency: this._agency,
campaignId: this._campaignId,
created: this._created,
activate: this._activate,
};
}
get name() {
return this._name;
}
get agency() {
return this._agency;
}
get company() {
return this._company;
}
get created() {
return this._created;
}
get activate() {
return this._activate;
}
get campaignId() {
return this._campaignId;
}
}
exports.Campaign = Campaign;
76 changes: 76 additions & 0 deletions dist/models/DAO/CampaignDAO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CampaignDAO = void 0;
const FirestoreConnectionSingleton_1 = require("../cloud/FirestoreConnectionSingleton");
class CampaignDAO {
constructor(campaign, agency) {
this._campaignName = campaign;
this._agency = agency;
this._objectStore = FirestoreConnectionSingleton_1.FirestoreConnectionSingleton.getInstance();
this._pathToCollection = ['campaigns'];
this._authCollection = this._objectStore.getCollection(this._pathToCollection);
}
addCampaign(campaign) {
return this._objectStore
.addDocumentIn(this._authCollection, campaign.toJson(), campaign.name)
.get()
.then(() => {
return true;
})
.catch((err) => {
console.log(err);
return false;
});
}
deactivateCampaign(campaignName, agency, userRequestPermission) {
return this._objectStore
.getCollection(this._pathToCollection)
.doc(campaignName)
.get()
.then((doc) => {
const campaign = doc.data();
if (campaign.agency === agency &&
(userRequestPermission === 'admin' ||
userRequestPermission === 'owner' ||
userRequestPermission === 'agencyOwner')) {
campaign.activate = false;
}
else {
throw new Error('Permissões insuficientes para inavitar a campanha!');
}
return doc.ref.set(campaign);
})
.then(() => {
return true;
})
.catch((err) => {
throw err;
});
}
reactivateCampaign(campaignName, agency, userRequestPermission) {
return this._objectStore
.getCollection(this._pathToCollection)
.doc(campaignName)
.get()
.then((doc) => {
const campaign = doc.data();
if (campaign.agency === agency &&
(userRequestPermission === 'admin' ||
userRequestPermission === 'owner' ||
userRequestPermission === 'agencyOwner')) {
campaign.activate = true;
}
else {
throw new Error('Permissões insuficientes para reativar a campanha!');
}
return doc.ref.set(campaign);
})
.then(() => {
return true;
})
.catch((err) => {
throw err;
});
}
}
exports.CampaignDAO = CampaignDAO;
59 changes: 36 additions & 23 deletions dist/models/RoutesPermission.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoutesPermission = void 0;
class RoutesPermission {
constructor(route, method) {
this._method = method;
this._route = route;
}
validatePermission(user) {
const agencyPostRoutes = ['/build/.*', '/csv', '/user/changepass', '/logout', '/login', '/campaign/add'];
const agencyGetRoutes = ['/config', '/template', '/csv/list', '/csv', '/user', '/campaign/list', '/campaign/teste'];
if (user.permission === 'user') {
if (this._method === 'POST') {
return agencyPostRoutes.filter((route) => new RegExp(route).test(this._route)).length > 0;
} else if (this._method === 'GET') {
return agencyGetRoutes.filter((route) => new RegExp(route).test(this._route)).length > 0;
} else {
return false;
}
} else if (user.permission === 'admin' || user.permission === 'owner') {
return true;
} else {
return false;
}
}
constructor(route, method) {
this._method = method;
this._route = route;
}
validatePermission(user) {
const agencyPostRoutes = [
'/build/.*',
'/csv',
'/user/changepass',
'/logout',
'/login',
'/campaign/add',
'/campaign/deactivate',
'/campaign/reactivate',
];
const agencyGetRoutes = ['/config', '/template', '/csv/list', '/csv', '/user', '/campaign/list', '/campaign/teste'];
if (user.permission === 'user') {
if (this._method === 'POST') {
return agencyPostRoutes.filter((route) => new RegExp(route).test(this._route)).length > 0;
}
else if (this._method === 'GET') {
return agencyGetRoutes.filter((route) => new RegExp(route).test(this._route)).length > 0;
}
else {
return false;
}
}
else if (user.permission === 'admin' || user.permission === 'owner') {
return true;
}
else {
return false;
}
}
}
exports.RoutesPermission = RoutesPermission;
104 changes: 71 additions & 33 deletions dist/routes/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", { value: true });
const ApiResponse_1 = require("../models/ApiResponse");
const FirestoreConnectionSingleton_1 = require("../models/cloud/FirestoreConnectionSingleton");
const FileDAO_1 = require("../models/DAO/FileDAO");
const firestore_1 = require("@google-cloud/firestore");
const CampaignDAO_1 = require("../models/DAO/CampaignDAO");
const Campaign_1 = require("../models/Campaign");
const campaign = (app) => {
app.get('/campaign', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const apiResponse = new ApiResponse_1.ApiResponse();
Expand Down Expand Up @@ -51,7 +51,13 @@ const campaign = (app) => {
const campaign = req.headers.campaign;
const permission = req.permission;
const fileDAO = new FileDAO_1.FileDAO();
if (!campaign) {
if ((permission !== 'admin' || permission !== 'owner') && !agency) {
apiResponse.responseText = 'Nenhuma agência foi informada!';
apiResponse.statusCode = 400;
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
return;
}
else if (!campaign) {
apiResponse.responseText = 'Nenhuma campanha foi informada!';
apiResponse.statusCode = 400;
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
Expand All @@ -76,53 +82,85 @@ const campaign = (app) => {
}));
app.post('/campaign/add', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const apiResponse = new ApiResponse_1.ApiResponse();
const firestore = new firestore_1.Firestore();
const firestoreCollection = firestore.collection('campaigns');
const firestoreConnectionInstance = FirestoreConnectionSingleton_1.FirestoreConnectionSingleton.getInstance();
const today = new Date();
const day = String(today.getDate()).padStart(2, '0');
const month = String(today.getMonth() + 1).padStart(2, '0');
const year = today.getFullYear();
const campaign = req.headers.campaign;
const values = {
created: `${year}-${month}-${day}`,
company: req.company,
agency: req.agency ? req.agency : 'CompanyCampaigns',
activated: true,
};
new Promise((resolve, reject) => {
if (values) {
apiResponse.responseText = JSON.stringify(values);
const created = `${year}-${month}-${day}`;
const campaignName = req.headers.campaign;
const company = req.company;
const agency = req.agency ? req.agency : 'CompanyCampaigns';
const campaignId = Date.now().toString(16);
const campaignObject = new Campaign_1.Campaign(campaignName, company, agency, campaignId, true, created);
new CampaignDAO_1.CampaignDAO()
.addCampaign(campaignObject)
.then((result) => {
if (result) {
apiResponse.statusCode = 200;
resolve('Campanha criada');
apiResponse.responseText = 'Campanha criada com sucesso!';
}
else {
apiResponse.statusCode = 400;
apiResponse.responseText = JSON.stringify(values);
reject('Criação da campanha falhou!');
throw new Error('Erro ao criar campanha!');
}
})
.then(() => {
firestoreConnectionInstance.addDocumentIn(firestoreCollection, values, campaign);
})
.catch((message) => {
throw message;
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
}));
app.get('/campaign/teste', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
app.post('/campaign/deactivate', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const apiResponse = new ApiResponse_1.ApiResponse();
res.status(apiResponse.statusCode).send(req);
const campaign = req.headers.campaign;
const agency = req.agency ? req.agency : 'CompanyCampaigns';
const permission = 'agencyOwner';
new CampaignDAO_1.CampaignDAO()
.deactivateCampaign(campaign, agency, permission)
.then((result) => {
if (result) {
apiResponse.statusCode = 200;
apiResponse.responseText = 'Campanha desativada com sucesso!';
}
else {
throw new Error('Erro ao desativar campanha!');
}
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
}));
app.post('/user/:id/deactivate', (req, res) => {
const apiResponse = new ApiResponse_1.ApiResponse();
const targetUserId = req.params.id;
});
app.post('/user/:id/reactivate', (req, res) => {
app.post('/campaign/reactivate', (req, res) => {
const apiResponse = new ApiResponse_1.ApiResponse();
const targetUserId = req.params.id;
const campaign = req.headers.campaign;
const agency = req.agency ? req.agency : 'CompanyCampaigns';
const permission = 'agencyOwner';
new CampaignDAO_1.CampaignDAO()
.reactivateCampaign(campaign, agency, permission)
.then((result) => {
if (result) {
apiResponse.statusCode = 200;
apiResponse.responseText = 'Campanha reativada com sucesso!';
}
else {
throw new Error('Erro ao reativar campanha!');
}
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
});
};
exports.default = campaign;
57 changes: 57 additions & 0 deletions src/ts/models/Campaign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { RoutesPermission } from './RoutesPermission';

export class Campaign {
private _name: string;
private _company: string;
private _agency: string;
private _campaignId: string;
private _activate: boolean;
private _created: string;

constructor(name: string, company: string, agency: string, campaignId: string, activate = true, created: string) {
this._name = name;
this._company = company;
this._agency = agency;
this._campaignId = campaignId;
this._activate = activate;
this._created = created;
}

/**
* Gera um JSON correspondente ao objeto Campaign
* @returns JSON correspondente ao objeto Campaign
*/
public toJson(): { [key: string]: string | boolean } {
return {
company: this._company,
agency: this._agency,
campaignId: this._campaignId,
created: this._created,
activate: this._activate,
};
}

get name(): string {
return this._name;
}

get agency(): string {
return this._agency;
}

get company(): string {
return this._company;
}

get created(): string {
return this._created;
}

get activate(): boolean {
return this._activate;
}

get campaignId(): string {
return this._campaignId;
}
}
Loading

0 comments on commit c9b4195

Please sign in to comment.