Skip to content

Commit

Permalink
improve: remove teams when deleting a ministry (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himali-Malvawala authored Aug 3, 2024
1 parent a5ce6fd commit 79e2e98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/controllers/GroupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import express from "express";
import { MembershipBaseController } from "./MembershipBaseController"
import { Group } from "../models"
import { Permissions } from '../helpers/Permissions'
import { ArrayHelper } from "@churchapps/apihelper";

@controller("/groups")
export class GroupController extends MembershipBaseController {
Expand Down Expand Up @@ -62,7 +63,18 @@ export class GroupController extends MembershipBaseController {
public async delete(@requestParam("id") id: string, req: express.Request<{}, {}, null>, res: express.Response): Promise<interfaces.IHttpActionResult> {
return this.actionWrapper(req, res, async (au) => {
if (!au.checkAccess(Permissions.groups.edit)) return this.json({}, 401);
else await this.repositories.group.delete(au.churchId, id);
else {
const group: Group = await this.repositories.group.load(au.churchId, id);
if (group.tags.indexOf("ministry") > -1) {
const AllTeams = await this.repositories.group.loadByTag(au.churchId, "team");
const ministryTeams = ArrayHelper.getAll(AllTeams, "categoryName", id);
const ids = ArrayHelper.getIds(ministryTeams, "id");
await this.repositories.group.delete(au.churchId, id);
await this.repositories.group.deleteByIds(au.churchId, ids);
} else {
await this.repositories.group.delete(au.churchId, id);
}
}
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/repositories/GroupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class GroupRepository {
return DB.query("UPDATE `groups` SET removed=1 WHERE id=? AND churchId=?;", [id, churchId]);
}

public deleteByIds(churchId: string, ids: string[]) {
return DB.query("UPDATE `groups` SET removed=1 WHERE id IN (?) AND churchId=?;", [ids, churchId]);
}

public load(churchId: string, id: string) {
return DB.queryOne("SELECT * FROM `groups` WHERE id=? AND churchId=? AND removed=0;", [id, churchId]);
}
Expand Down

0 comments on commit 79e2e98

Please sign in to comment.