Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refatoração das Classes DAO #314

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions dist/models/DAO/AdOpsTeamDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AdOpsTeamDAO {
throw err;
});
}
getAllAdOpsTeamsFrom(advertiser, permission) {
getAllAdOpsTeamsFrom(advertiser) {
const equal = '==';
const conditions = [
{
Expand All @@ -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;
Expand Down
124 changes: 81 additions & 43 deletions dist/models/DAO/CampaignDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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;
});
Expand Down
7 changes: 2 additions & 5 deletions dist/models/DAO/UserDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ 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) => {
const users = [];
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,
Expand Down
1 change: 0 additions & 1 deletion dist/models/RoutesPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RoutesPermission {
'/csv',
'/user',
'/campaign/.*/list',
'/adOpsTeam/list',
'/campaign/.*/csv/list',
'/adOpsTeams/campaigns',
];
Expand Down
2 changes: 1 addition & 1 deletion dist/routes/adOpsTeam.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
})
Expand Down
143 changes: 90 additions & 53 deletions dist/routes/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion dist/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
})
Expand Down
Loading