diff --git a/dist/models/DAO/AdOpsTeamDAO.js b/dist/models/DAO/AdOpsTeamDAO.js index b672949..6d2d5a9 100644 --- a/dist/models/DAO/AdOpsTeamDAO.js +++ b/dist/models/DAO/AdOpsTeamDAO.js @@ -39,7 +39,7 @@ class AdOpsTeamDAO { throw err; }); } - getAllAdOpsTeamsFrom(advertiser, permission) { + getAllAdOpsTeamsFrom(advertiser) { const equal = '=='; const conditions = [ { @@ -61,9 +61,7 @@ class AdOpsTeamDAO { ) ); }); - return permission !== 'owner' && permission !== 'admin' - ? adOpsTeams.filter((AdOpsTeam) => AdOpsTeam.name !== 'Campanhas Internas') - : adOpsTeams; + return adOpsTeams; }) .catch((err) => { throw err; diff --git a/dist/models/DAO/CampaignDAO.js b/dist/models/DAO/CampaignDAO.js index 2ff8a64..5ee3c81 100644 --- a/dist/models/DAO/CampaignDAO.js +++ b/dist/models/DAO/CampaignDAO.js @@ -41,19 +41,19 @@ class CampaignDAO { this._campaignCollection = this._objectStore.getCollection(this._pathToCollection); } getCampaign(campaignId) { - return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (campaigns.length > 0) { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - return filteredCampaign.name; - } else { - throw new Error('Nenhuma campanha encontrada!'); - } - }) - .catch((err) => { - throw err; - }); + const equal = '=='; + const conditions = [ + { + key: 'campaignId', + operator: equal, + value: campaignId, + }, + ]; + return this._objectStore.getDocumentFiltered(this._campaignCollection, conditions).then((campaigns) => { + if (campaigns.docs.length > 0) { + return campaigns.docs[0].id; + } + }); } getCampaignById(campaignId) { return this._objectStore @@ -73,6 +73,37 @@ class CampaignDAO { throw err; }); } + getAllCampaigns(advertiser) { + const equal = '=='; + const conditions = [ + { + key: 'advertiser', + operator: equal, + value: advertiser, + }, + ]; + return this._objectStore + .getDocumentFiltered(this._campaignCollection, conditions) + .then((campaignsDocuments) => { + const campanha = []; + campaignsDocuments.docs.map((campaignsDocument) => { + campanha.push( + new Campaign_1.Campaign( + campaignsDocument.get('name'), + campaignsDocument.get('advertiser'), + campaignsDocument.get('adOpsTeam'), + campaignsDocument.get('campaignId'), + campaignsDocument.get('active'), + campaignsDocument.get('created') + ) + ); + }); + return campanha; + }) + .catch((err) => { + throw err; + }); + } getAllCampaignsFrom(adOpsTeam, userRequestPermission) { return this._objectStore .getAllDocumentsFrom(this._campaignCollection) @@ -115,6 +146,37 @@ class CampaignDAO { return false; }); } + getAdopsteamCampaign(campaignId) { + const equal = '=='; + const conditions = [ + { + key: 'campaignId', + operator: equal, + value: campaignId, + }, + ]; + return this._objectStore + .getDocumentFiltered(this._campaignCollection, conditions) + .then((campaignsDocuments) => { + const campanha = []; + campaignsDocuments.docs.map((campaignsDocument) => { + campanha.push( + new Campaign_1.Campaign( + campaignsDocument.get('name'), + campaignsDocument.get('advertiser'), + campaignsDocument.get('adOpsTeam'), + campaignsDocument.get('campaignId'), + campaignsDocument.get('active'), + campaignsDocument.get('created') + ) + ); + }); + return campanha[0].adOpsTeam; + }) + .catch((err) => { + throw err; + }); + } deleteCampaign(campaignId) { return this._objectStore .deleteDocumentById(this._campaignCollection, campaignId) @@ -125,42 +187,18 @@ class CampaignDAO { return false; }); } - deactivateCampaign(campaignId, userRequestPermission) { + deactivateCampaign(campaignId) { return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (userRequestPermission !== 'user') { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - filteredCampaign.active = false; - return filteredCampaign; - } else { - throw new Error('Permissões insuficientes para inavitar a campanha!'); - } - }) - .then((filteredCampaign) => { - this._objectStore.getCollection(this._pathToCollection).doc(campaignId).update(filteredCampaign); - return true; - }) + .updateDocumentById(this._campaignCollection, campaignId, { active: false }) + .then(() => true) .catch((err) => { throw err; }); } - reactivateCampaign(campaignId, userRequestPermission) { + reactivateCampaign(campaignId) { return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (userRequestPermission !== 'user') { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - filteredCampaign.active = true; - return filteredCampaign; - } else { - throw new Error('Permissões insuficientes para inavitar a campanha!'); - } - }) - .then((filteredCampaign) => { - this._objectStore.getCollection(this._pathToCollection).doc(campaignId).update(filteredCampaign); - return true; - }) + .updateDocumentById(this._campaignCollection, campaignId, { active: true }) + .then(() => true) .catch((err) => { throw err; }); diff --git a/dist/models/DAO/UserDAO.js b/dist/models/DAO/UserDAO.js index 11a6126..ec453bb 100644 --- a/dist/models/DAO/UserDAO.js +++ b/dist/models/DAO/UserDAO.js @@ -43,7 +43,7 @@ class UserDAO { this._pathToCollection = ['users']; this._userCollection = this._objectStore.getCollection(this._pathToCollection); } - getAllUsersFrom(advertiser, userRequestPermission) { + getAllUsersFrom(advertiser) { return this._objectStore .getAllDocumentsFrom(this._userCollection) .then((allUsersDocuments) => { @@ -51,10 +51,7 @@ class UserDAO { const allAdvertiserUsers = allUsersDocuments.filter((user) => user.advertiser === advertiser); if (allAdvertiserUsers.length > 0) { allAdvertiserUsers.forEach((advertiserUser) => { - if ( - advertiserUser.permission !== 'owner' || - (userRequestPermission === 'admin' && advertiserUser.permission === 'user') - ) { + if (advertiserUser.permission === 'user') { const user = new User_1.User( advertiserUser.userid, advertiserUser.permission, diff --git a/dist/models/RoutesPermission.js b/dist/models/RoutesPermission.js index 73f9018..16533d7 100644 --- a/dist/models/RoutesPermission.js +++ b/dist/models/RoutesPermission.js @@ -15,7 +15,6 @@ class RoutesPermission { '/csv', '/user', '/campaign/.*/list', - '/adOpsTeam/list', '/campaign/.*/csv/list', '/adOpsTeams/campaigns', ]; diff --git a/dist/routes/adOpsTeam.js b/dist/routes/adOpsTeam.js index 0d10b91..82e6b1a 100644 --- a/dist/routes/adOpsTeam.js +++ b/dist/routes/adOpsTeam.js @@ -92,7 +92,7 @@ const adOpsTeam = (app) => { const advertiser = req.advertiser; const permission = req.permission; new AdOpsTeamDAO_1.AdOpsTeamDAO() - .getAllAdOpsTeamsFrom(advertiser, permission) + .getAllAdOpsTeamsFrom(advertiser) .then((adOpsTeams) => { apiResponse.responseText = JSON.stringify(adOpsTeams.map((adOpsTeam) => adOpsTeam.toJson())); }) diff --git a/dist/routes/campaign.js b/dist/routes/campaign.js index 5fae948..2d38781 100644 --- a/dist/routes/campaign.js +++ b/dist/routes/campaign.js @@ -86,21 +86,38 @@ const campaign = (app) => { app.get('/campaign/:adOpsTeam/list', (req, res) => __awaiter(void 0, void 0, void 0, function* () { const apiResponse = new ApiResponse_1.ApiResponse(); - const adOpsTeam = req.params.adOpsTeam !== 'Campanhas Internas' ? req.params.adOpsTeam : 'AdvertiserCampaigns'; + const adOpsTeam = req.params.adOpsTeam === ':adOpsTeam' ? '' : req.params.adOpsTeam; const permission = req.permission; - new CampaignDAO_1.CampaignDAO() - .getAllCampaignsFrom(adOpsTeam, permission) - .then((adOpsTeams) => { - apiResponse.responseText = JSON.stringify(adOpsTeams); - }) - .catch((err) => { - apiResponse.statusCode = 500; - apiResponse.responseText = err.message; - apiResponse.errorMessage = err.message; - }) - .finally(() => { - res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); + const advertiser = req.advertiser; + if ((req.permission === 'owner' || req.permission === 'admin') && !req.params.adOpsTeam) { + new CampaignDAO_1.CampaignDAO() + .getAllCampaigns(advertiser) + .then((campanha) => { + apiResponse.responseText = JSON.stringify(campanha); + }) + .catch((err) => { + apiResponse.statusCode = 500; + apiResponse.responseText = err.message; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { + new CampaignDAO_1.CampaignDAO() + .getAllCampaignsFrom(adOpsTeam, permission) + .then((adOpsTeams) => { + apiResponse.responseText = JSON.stringify(adOpsTeams); + }) + .catch((err) => { + apiResponse.statusCode = 500; + apiResponse.responseText = err.message; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } }) ); app.get('/:adOpsTeam/:campaignId/csv/list', (req, res) => @@ -179,48 +196,68 @@ const campaign = (app) => { const apiResponse = new ApiResponse_1.ApiResponse(); const campaignId = req.params.id; const permission = req.permission; - new CampaignDAO_1.CampaignDAO() - .deactivateCampaign(campaignId, 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 = 'Erro ao desativar campanha!'; - apiResponse.errorMessage = err.message; - }) - .finally(() => { - res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); + const userAdOpsTeam = req.adOpsTeam; + const AdopsCampaign = yield new CampaignDAO_1.CampaignDAO().getAdopsteamCampaign(campaignId); + if (permission != 'user' && userAdOpsTeam === AdopsCampaign) { + new CampaignDAO_1.CampaignDAO() + .deactivateCampaign(campaignId) + .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 = 'Erro ao desativar campanha!'; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { + apiResponse.statusCode = 500; + apiResponse.responseText = 'Erro ao desativar Campanha!'; + apiResponse.errorMessage = 'Erro ao desativar Campanha!'; + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + } }) ); - app.post('/campaign/:id/reactivate', (req, res) => { - const apiResponse = new ApiResponse_1.ApiResponse(); - const campaignId = req.params.id; - const permission = req.permission; - new CampaignDAO_1.CampaignDAO() - .reactivateCampaign(campaignId, permission) - .then((result) => { - if (result) { - apiResponse.statusCode = 200; - apiResponse.responseText = 'Campanha reativada com sucesso!'; - } else { - throw new Error('Erro ao reativar campanha!'); - } - }) - .catch((err) => { + app.post('/campaign/:id/reactivate', (req, res) => + __awaiter(void 0, void 0, void 0, function* () { + const apiResponse = new ApiResponse_1.ApiResponse(); + const campaignId = req.params.id; + const permission = req.permission; + const userAdOpsTeam = req.adOpsTeam; + const AdopsCampaign = yield new CampaignDAO_1.CampaignDAO().getAdopsteamCampaign(campaignId); + if (permission != 'user' && userAdOpsTeam === AdopsCampaign) { + new CampaignDAO_1.CampaignDAO() + .reactivateCampaign(campaignId) + .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 = 'Erro ao reativar campanha!'; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { apiResponse.statusCode = 500; - apiResponse.responseText = 'Erro ao reativar campanha!'; - apiResponse.errorMessage = err.message; - }) - .finally(() => { + apiResponse.responseText = 'Erro ao reativar Campanha!'; + apiResponse.errorMessage = 'Erro ao reativar Campanha!'; res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); - }); + } + }) + ); }; exports.default = campaign; diff --git a/dist/routes/user.js b/dist/routes/user.js index 4f9b8c7..5720fd4 100644 --- a/dist/routes/user.js +++ b/dist/routes/user.js @@ -6,7 +6,7 @@ const user = (app) => { app.get('/users', (req, res) => { const apiResponse = new ApiResponse_1.ApiResponse(); new UserDAO_1.UserDAO() - .getAllUsersFrom(req.advertiser, req.permission) + .getAllUsersFrom(req.advertiser) .then((users) => { apiResponse.responseText = JSON.stringify(users.map((user) => user.toJson())); }) diff --git a/src/ts/models/DAO/AdOpsTeamDAO.ts b/src/ts/models/DAO/AdOpsTeamDAO.ts index 26c6592..542007c 100644 --- a/src/ts/models/DAO/AdOpsTeamDAO.ts +++ b/src/ts/models/DAO/AdOpsTeamDAO.ts @@ -61,7 +61,7 @@ export class AdOpsTeamDAO { * @param userRequestPermission permissão do usuario que solicitou a alteração * @returns Lista Objetos contendo atributos de cada campanha */ - public getAllAdOpsTeamsFrom(advertiser: string, permission: string): Promise { + public getAllAdOpsTeamsFrom(advertiser: string): Promise { const equal: WhereFilterOp = '=='; const conditions = [ { @@ -83,9 +83,7 @@ export class AdOpsTeamDAO { ) ); }); - return permission !== 'owner' && permission !== 'admin' - ? adOpsTeams.filter((AdOpsTeam) => AdOpsTeam.name !== 'Campanhas Internas') - : adOpsTeams; + return adOpsTeams; }) .catch((err) => { throw err; diff --git a/src/ts/models/DAO/CampaignDAO.ts b/src/ts/models/DAO/CampaignDAO.ts index 0c261aa..cc18108 100644 --- a/src/ts/models/DAO/CampaignDAO.ts +++ b/src/ts/models/DAO/CampaignDAO.ts @@ -20,19 +20,19 @@ export class CampaignDAO { * @returns Retorna campanha procurada */ public getCampaign(campaignId: string): Promise { - return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (campaigns.length > 0) { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - return filteredCampaign.name; - } else { - throw new Error('Nenhuma campanha encontrada!'); - } - }) - .catch((err) => { - throw err; - }); + const equal: WhereFilterOp = '=='; + const conditions = [ + { + key: 'campaignId', + operator: equal, + value: campaignId, + }, + ]; + return this._objectStore.getDocumentFiltered(this._campaignCollection, conditions).then((campaigns) => { + if (campaigns.docs.length > 0) { + return campaigns.docs[0].id; + } + }); } /** @@ -60,6 +60,43 @@ export class CampaignDAO { }); } + /** + * retorna todas as campanhas de todos os adopsteam de um advertiser. + * @param advertiser do usuario onde vão ser pegado as campanhas. + * @returns Lista Objetos contendo atributos de cada campanha + */ + public getAllCampaigns(advertiser: string): Promise { + const equal: WhereFilterOp = '=='; + const conditions = [ + { + key: 'advertiser', + operator: equal, + value: advertiser, + }, + ]; + return this._objectStore + .getDocumentFiltered(this._campaignCollection, conditions) + .then((campaignsDocuments) => { + const campanha: Campaign[] = []; + campaignsDocuments.docs.map((campaignsDocument) => { + campanha.push( + new Campaign( + campaignsDocument.get('name'), + campaignsDocument.get('advertiser'), + campaignsDocument.get('adOpsTeam'), + campaignsDocument.get('campaignId'), + campaignsDocument.get('active'), + campaignsDocument.get('created') + ) + ); + }); + return campanha; + }) + .catch((err) => { + throw err; + }); + } + /** * Retorna todas as adOpsTeams de um advertiser * @param adOpsTeam adOpsTeam das campanhas a serem buscados @@ -119,6 +156,43 @@ export class CampaignDAO { }); } + /** + * Retorna o adopsteam de uma campanha + * @param CampaignId ID da campanha a ser buscada + * @retuns retorna a adopsteam da campanha + */ + public getAdopsteamCampaign(campaignId: string): Promise { + const equal: WhereFilterOp = '=='; + const conditions = [ + { + key: 'campaignId', + operator: equal, + value: campaignId, + }, + ]; + return this._objectStore + .getDocumentFiltered(this._campaignCollection, conditions) + .then((campaignsDocuments) => { + const campanha: Campaign[] = []; + campaignsDocuments.docs.map((campaignsDocument) => { + campanha.push( + new Campaign( + campaignsDocument.get('name'), + campaignsDocument.get('advertiser'), + campaignsDocument.get('adOpsTeam'), + campaignsDocument.get('campaignId'), + campaignsDocument.get('active'), + campaignsDocument.get('created') + ) + ); + }); + return campanha[0].adOpsTeam; + }) + .catch((err) => { + throw err; + }); + } + /** * Deleta uma campanha * @param campaignId campanha que será deletada @@ -141,22 +215,10 @@ export class CampaignDAO { * @param userRequestPermission permissão do usuario que solicitou a alteração * @returns retorna True em caso de sucesso */ - public deactivateCampaign(campaignId: string, userRequestPermission: string): Promise { + public deactivateCampaign(campaignId: string): Promise { return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (userRequestPermission !== 'user') { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - filteredCampaign.active = false; - return filteredCampaign; - } else { - throw new Error('Permissões insuficientes para inavitar a campanha!'); - } - }) - .then((filteredCampaign) => { - this._objectStore.getCollection(this._pathToCollection).doc(campaignId).update(filteredCampaign); - return true; - }) + .updateDocumentById(this._campaignCollection, campaignId, { active: false }) + .then(() => true) .catch((err) => { throw err; }); @@ -168,22 +230,10 @@ export class CampaignDAO { * @param userRequestPermission permissão do usuario que solicitou a alteração * @returns retorna True em caso de sucesso */ - public reactivateCampaign(campaignId: string, userRequestPermission: string): Promise { + public reactivateCampaign(campaignId: string): Promise { return this._objectStore - .getAllDocumentsFrom(this._campaignCollection) - .then((campaigns) => { - if (userRequestPermission !== 'user') { - const [filteredCampaign] = campaigns.filter((campaign) => campaign.campaignId === campaignId); - filteredCampaign.active = true; - return filteredCampaign; - } else { - throw new Error('Permissões insuficientes para inavitar a campanha!'); - } - }) - .then((filteredCampaign) => { - this._objectStore.getCollection(this._pathToCollection).doc(campaignId).update(filteredCampaign); - return true; - }) + .updateDocumentById(this._campaignCollection, campaignId, { active: true }) + .then(() => true) .catch((err) => { throw err; }); diff --git a/src/ts/models/DAO/UserDAO.ts b/src/ts/models/DAO/UserDAO.ts index 94076dd..21b6693 100644 --- a/src/ts/models/DAO/UserDAO.ts +++ b/src/ts/models/DAO/UserDAO.ts @@ -28,7 +28,7 @@ export class UserDAO { * @param userRequestPermission permissão do usuario que solicitou a alteração * @returns Lista de usuários */ - public getAllUsersFrom(advertiser: string, userRequestPermission: string): Promise { + public getAllUsersFrom(advertiser: string): Promise { return this._objectStore .getAllDocumentsFrom(this._userCollection) .then((allUsersDocuments) => { @@ -36,10 +36,7 @@ export class UserDAO { const allAdvertiserUsers = allUsersDocuments.filter((user) => user.advertiser === advertiser); if (allAdvertiserUsers.length > 0) { allAdvertiserUsers.forEach((advertiserUser) => { - if ( - advertiserUser.permission !== 'owner' || - (userRequestPermission === 'admin' && advertiserUser.permission === 'user') - ) { + if (advertiserUser.permission === 'user') { const user = new User( advertiserUser.userid, advertiserUser.permission, diff --git a/src/ts/models/RoutesPermission.ts b/src/ts/models/RoutesPermission.ts index 8466a23..db24999 100644 --- a/src/ts/models/RoutesPermission.ts +++ b/src/ts/models/RoutesPermission.ts @@ -18,7 +18,6 @@ export class RoutesPermission { '/csv', '/user', '/campaign/.*/list', - '/adOpsTeam/list', '/campaign/.*/csv/list', '/adOpsTeams/campaigns', ]; diff --git a/src/ts/routes/adOpsTeam.ts b/src/ts/routes/adOpsTeam.ts index 5b1b03d..3695625 100644 --- a/src/ts/routes/adOpsTeam.ts +++ b/src/ts/routes/adOpsTeam.ts @@ -66,7 +66,7 @@ const adOpsTeam = (app: { [key: string]: any }): void => { const permission = req.permission; new AdOpsTeamDAO() - .getAllAdOpsTeamsFrom(advertiser, permission) + .getAllAdOpsTeamsFrom(advertiser) .then((adOpsTeams: AdOpsTeam[]) => { apiResponse.responseText = JSON.stringify(adOpsTeams.map((adOpsTeam) => adOpsTeam.toJson())); }) diff --git a/src/ts/routes/campaign.ts b/src/ts/routes/campaign.ts index ecef2f8..3cd9ade 100644 --- a/src/ts/routes/campaign.ts +++ b/src/ts/routes/campaign.ts @@ -58,23 +58,39 @@ const campaign = (app: { [key: string]: any }): void => { app.get('/campaign/:adOpsTeam/list', async (req: { [key: string]: any }, res: { [key: string]: any }) => { const apiResponse = new ApiResponse(); - - const adOpsTeam = req.params.adOpsTeam !== 'Campanhas Internas' ? req.params.adOpsTeam : 'AdvertiserCampaigns'; + const adOpsTeam = req.params.adOpsTeam === ':adOpsTeam' ? '' : req.params.adOpsTeam; const permission = req.permission; + const advertiser = req.advertiser; - new CampaignDAO() - .getAllCampaignsFrom(adOpsTeam, permission) - .then((adOpsTeams: { campaignName: string; campaignId: string }[]) => { - apiResponse.responseText = JSON.stringify(adOpsTeams); - }) - .catch((err) => { - apiResponse.statusCode = 500; - apiResponse.responseText = err.message; - apiResponse.errorMessage = err.message; - }) - .finally(() => { - res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); + if ((req.permission === 'owner' || req.permission === 'admin') && !req.params.adOpsTeam) { + new CampaignDAO() + .getAllCampaigns(advertiser) + .then((campanha: Campaign[]) => { + apiResponse.responseText = JSON.stringify(campanha); + }) + .catch((err) => { + apiResponse.statusCode = 500; + apiResponse.responseText = err.message; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { + new CampaignDAO() + .getAllCampaignsFrom(adOpsTeam, permission) + .then((adOpsTeams: { campaignName: string; campaignId: string }[]) => { + apiResponse.responseText = JSON.stringify(adOpsTeams); + }) + .catch((err) => { + apiResponse.statusCode = 500; + apiResponse.responseText = err.message; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } }); app.get('/:adOpsTeam/:campaignId/csv/list', async (req: { [key: string]: any }, res: { [key: string]: any }) => { @@ -159,50 +175,68 @@ const campaign = (app: { [key: string]: any }): void => { const apiResponse = new ApiResponse(); const campaignId = req.params.id; const permission = req.permission; - - new CampaignDAO() - .deactivateCampaign(campaignId, permission) - .then((result: boolean) => { - 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 = 'Erro ao desativar campanha!'; - apiResponse.errorMessage = err.message; - }) - .finally(() => { - res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); + const userAdOpsTeam = req.adOpsTeam; + const AdopsCampaign = await new CampaignDAO().getAdopsteamCampaign(campaignId); + + if (permission != 'user' && userAdOpsTeam === AdopsCampaign) { + new CampaignDAO() + .deactivateCampaign(campaignId) + .then((result: boolean) => { + 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 = 'Erro ao desativar campanha!'; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { + apiResponse.statusCode = 500; + apiResponse.responseText = 'Erro ao desativar Campanha!'; + apiResponse.errorMessage = 'Erro ao desativar Campanha!'; + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + } }); - app.post('/campaign/:id/reactivate', (req: { [key: string]: any }, res: { [key: string]: any }) => { + app.post('/campaign/:id/reactivate', async (req: { [key: string]: any }, res: { [key: string]: any }) => { const apiResponse = new ApiResponse(); const campaignId = req.params.id; const permission = req.permission; - - new CampaignDAO() - .reactivateCampaign(campaignId, permission) - .then((result: boolean) => { - 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 = 'Erro ao reativar campanha!'; - apiResponse.errorMessage = err.message; - }) - .finally(() => { - res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); - }); + const userAdOpsTeam = req.adOpsTeam; + const AdopsCampaign = await new CampaignDAO().getAdopsteamCampaign(campaignId); + + if (permission != 'user' && userAdOpsTeam === AdopsCampaign) { + new CampaignDAO() + .reactivateCampaign(campaignId) + .then((result: boolean) => { + 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 = 'Erro ao reativar campanha!'; + apiResponse.errorMessage = err.message; + }) + .finally(() => { + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + }); + } else { + apiResponse.statusCode = 500; + apiResponse.responseText = 'Erro ao reativar Campanha!'; + apiResponse.errorMessage = 'Erro ao reativar Campanha!'; + res.status(apiResponse.statusCode).send(apiResponse.jsonResponse); + } }); }; diff --git a/src/ts/routes/user.ts b/src/ts/routes/user.ts index ad5eede..89fbd2b 100644 --- a/src/ts/routes/user.ts +++ b/src/ts/routes/user.ts @@ -6,7 +6,7 @@ const user = (app: { [key: string]: any }): void => { app.get('/users', (req: { [key: string]: any }, res: { [key: string]: any }) => { const apiResponse = new ApiResponse(); new UserDAO() - .getAllUsersFrom(req.advertiser, req.permission) + .getAllUsersFrom(req.advertiser) .then((users: User[]) => { apiResponse.responseText = JSON.stringify(users.map((user: User) => user.toJson())); })