-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Ajoute un endpoint pour supprimer des campagnes d'une organ…
- Loading branch information
Showing
24 changed files
with
979 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
api/src/prescription/campaign/domain/models/CampaignsDestructor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ObjectValidationError } from '../../../../../lib/domain/errors.js'; | ||
|
||
/** | ||
* @typedef {import ('./Campaign.js').Campaign} Campaign | ||
* @typedef {import ('../../../campaign-participation/domain/models/CampaignParticipation.js').CampaignParticipation} CampaignParticipation | ||
* @typedef {import ('../read-models/OrganizationMembership.js').OrganizationMembership} OrganizationMembership | ||
*/ | ||
|
||
class CampaignsDestructor { | ||
#campaignsToDelete; | ||
#campaignParticipationsToDelete; | ||
#userId; | ||
#organizationId; | ||
#membership; | ||
|
||
/** | ||
* @param {Object} params | ||
* @param {Array<Campaign>} params.campaignsToDelete - campaigns object to be deleted | ||
* @param {Array<CampaignParticipation>} params.campaignParticipationsToDelete - campaigns participations object to be deleted | ||
* @param {number} params.userId - userId for deletedBy | ||
* @param {number} params.organizationId - organizationId to check if campaigns belongs to given organizationId | ||
* @param {OrganizationMembership} params.membership - class with property isAdmin to check is user is admin in organization or not | ||
*/ | ||
constructor({ campaignsToDelete, campaignParticipationsToDelete, userId, organizationId, membership }) { | ||
this.#campaignsToDelete = campaignsToDelete; | ||
this.#campaignParticipationsToDelete = campaignParticipationsToDelete; | ||
this.#userId = userId; | ||
this.#organizationId = organizationId; | ||
this.#membership = membership; | ||
this.#validate(); | ||
} | ||
|
||
#validate() { | ||
const isUserOwnerOfAllCampaigns = this.#campaignsToDelete.every((campaign) => campaign.ownerId === this.#userId); | ||
const isAllCampaignsBelongsToOrganization = this.#campaignsToDelete.every( | ||
(campaign) => campaign.organizationId === this.#organizationId, | ||
); | ||
|
||
if (!isAllCampaignsBelongsToOrganization) | ||
throw new ObjectValidationError('Some campaigns does not belong to organization.'); | ||
if (!this.#membership.isAdmin && !isUserOwnerOfAllCampaigns) | ||
throw new ObjectValidationError('User does not have right to delete some campaigns.'); | ||
} | ||
|
||
delete() { | ||
this.#campaignParticipationsToDelete.forEach((campaignParticipation) => campaignParticipation.delete(this.#userId)); | ||
this.#campaignsToDelete.forEach((campaign) => campaign.delete(this.#userId)); | ||
} | ||
|
||
get campaignParticipations() { | ||
return this.#campaignParticipationsToDelete; | ||
} | ||
|
||
get campaigns() { | ||
return this.#campaignsToDelete; | ||
} | ||
} | ||
|
||
export { CampaignsDestructor }; |
7 changes: 7 additions & 0 deletions
7
api/src/prescription/campaign/domain/read-models/OrganizationMembership.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class OrganizationMembership { | ||
constructor({ isAdmin } = {}) { | ||
this.isAdmin = isAdmin; | ||
} | ||
} | ||
|
||
export { OrganizationMembership }; |
28 changes: 28 additions & 0 deletions
28
api/src/prescription/campaign/domain/usecases/delete-campaigns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { CampaignsDestructor } from '../models/CampaignsDestructor.js'; | ||
|
||
const deleteCampaigns = async ({ | ||
userId, | ||
organizationId, | ||
campaignIds, | ||
organizationMembershipRepository, | ||
campaignAdministrationRepository, | ||
campaignParticipationRepository, | ||
}) => { | ||
const membership = await organizationMembershipRepository.getByUserIdAndOrganizationId({ userId, organizationId }); | ||
const campaignsToDelete = await campaignAdministrationRepository.getByIds(campaignIds); | ||
const campaignParticipationsToDelete = await campaignParticipationRepository.getByCampaignIds(campaignIds); | ||
|
||
const campaignDestructor = new CampaignsDestructor({ | ||
campaignsToDelete, | ||
campaignParticipationsToDelete, | ||
userId, | ||
organizationId, | ||
membership, | ||
}); | ||
campaignDestructor.delete(); | ||
|
||
await campaignParticipationRepository.batchUpdate(campaignParticipationsToDelete); | ||
await campaignAdministrationRepository.batchUpdate(campaignsToDelete); | ||
}; | ||
|
||
export { deleteCampaigns }; |
Oops, something went wrong.