Skip to content

Commit

Permalink
Merge pull request #149 from TogetherCrew/feature/announcement-caseca…
Browse files Browse the repository at this point in the history
…de-delete

Feature/announcement casecade delete
  • Loading branch information
Behzad-rabiei authored Jan 26, 2024
2 parents 1f4b678 + c1c6839 commit 82df715
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@togethercrew.dev/db",
"version": "3.0.28",
"version": "3.0.29",
"description": "All interactions with DB",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
35 changes: 34 additions & 1 deletion src/models/schemas/Platform.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,43 @@ const platformSchema = new Schema<IPlatform, PlatformModel>(
platformSchema.plugin(toJSON);
platformSchema.plugin(paginate);

const announcementDeletion = async (platformId: any): Promise<void> => {
// ?in case the platformID (inputted platformID) of each item in the data array field matches, delete that announcement.
const announcementsWithAllDataOnSamePlatformIds = await Announcement.aggregate([
{
$match: {
'data.platform': platformId,
},
},
{
$project: {
allMatch: {
$not: [
{
$elemMatch: {
'data.platform': { $ne: platformId },
},
},
],
},
},
},
{
$match: {
allMatch: true,
},
},
]);
const idsToDelete = announcementsWithAllDataOnSamePlatformIds.map((announcement) => announcement._id);
await Announcement.deleteMany({ _id: { $in: idsToDelete } });

await Announcement.updateMany({ 'data.platform': platformId }, { $pull: { data: { platform: platformId } } });
};

platformSchema.pre('remove', async function (this: Document) {
const platformId = this._id;
await Community.updateOne({ platforms: platformId }, { $pull: { platforms: platformId } });
await Announcement.updateMany({ 'data.platform': platformId }, { $pull: { data: { platform: platformId } } });
await announcementDeletion(platformId);
});

export default platformSchema;

0 comments on commit 82df715

Please sign in to comment.