Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #390 from beabee-communityrm/fix/delete-callout
Browse files Browse the repository at this point in the history
fix: deleting wrong callout
  • Loading branch information
wpf500 authored Apr 5, 2024
2 parents 293c8df + 45c67c3 commit 86b7435
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/api/controllers/CalloutController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class CalloutController {

@Authorized("admin")
@OnUndefined(204)
@Delete("/:slug")
@Delete("/:id")
async deleteCallout(@CalloutId() id: string): Promise<void> {
const deleted = await CalloutsService.deleteCallout(id);
if (!deleted) {
Expand Down
4 changes: 4 additions & 0 deletions src/api/decorators/CalloutId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function CalloutId() {
required: true,
value: async (action): Promise<string | undefined> => {
const id = action.request.params.id;
if (!id) {
throw new NotFoundError();
}

if (isUUID(id)) {
return id;
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/core/services/CalloutsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import NewsletterService from "@core/services/NewsletterService";
import OptionsService from "@core/services/OptionsService";

import { getRepository, runTransaction } from "@core/database";
import { log as mainLogger } from "@core/logging";
import { isDuplicateIndex } from "@core/utils";

import Contact from "@models/Contact";
Expand All @@ -22,12 +23,13 @@ import CalloutTag from "@models/CalloutTag";
import CalloutVariant from "@models/CalloutVariant";

import DuplicateId from "@api/errors/DuplicateId";

import InvalidCalloutResponse from "@api/errors/InvalidCalloutResponse";
import NotFoundError from "@api/errors/NotFoundError";

import { CalloutAccess } from "@enums/callout-access";
import { CreateCalloutData } from "@type/callout-data";
import NotFoundError from "@api/errors/NotFoundError";

const log = mainLogger.child({ app: "callouts-service" });

class CalloutsService {
/**
Expand All @@ -51,6 +53,7 @@ class CalloutsService {

while (true) {
const slug = baseSlug + (autoSlug ? "-" + autoSlug : "");
log.info("Creating callout with slug " + slug);
try {
return await this.saveCallout({ ...data, slug });
} catch (err) {
Expand All @@ -73,6 +76,7 @@ class CalloutsService {
id: string,
data: Partial<CreateCalloutData>
): Promise<void> {
log.info("Updating callout " + id);
// Prevent the join survey from being made inactive
if (OptionsService.getText("join-survey") === id) {
if (data.expires) {
Expand Down Expand Up @@ -127,6 +131,8 @@ class CalloutsService {
* @returns true if the callout was deleted
*/
async deleteCallout(id: string): Promise<boolean> {
log.info("Deleting callout " + id);

return await runTransaction(async (em) => {
await em
.createQueryBuilder()
Expand Down Expand Up @@ -342,6 +348,8 @@ class CalloutsService {
);
}

log.info("Saved callout " + newId);

return newId;
});
}
Expand Down

0 comments on commit 86b7435

Please sign in to comment.