-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-news-status.ts
45 lines (41 loc) · 1.43 KB
/
change-news-status.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use server";
import "server-only";
import { sendLineNotifyMessage } from "@/apis/line-notify/fetch-message";
import {
revertSelfNewsStatus,
updateSelfNewsStatus,
} from "@/apis/prisma/fetch-news";
import { SUCCESS_MESSAGES } from "@/constants";
import { NotAllowedError, UnexpectedError } from "@/error-classes";
import { wrapServerSideErrorForClient } from "@/error-wrapper";
import { checkUpdateStatusPermission } from "@/features/auth/utils/role";
import type { UpdateOrRevert } from "@/features/update-status/types";
import type { ServerAction } from "@/types";
import { formatChangeStatusMessage } from "@/utils/format-for-line";
const handleStatusChange = async (changeType: UpdateOrRevert) => {
switch (changeType) {
case "UPDATE":
return await updateSelfNewsStatus();
case "REVERT":
return await revertSelfNewsStatus();
default:
throw new UnexpectedError();
}
};
type ToastMessage = string;
export async function changeNewsStatus(
updateOrRevert: UpdateOrRevert,
): Promise<ServerAction<ToastMessage>> {
try {
const hasUpdateStatusPermission = await checkUpdateStatusPermission();
if (!hasUpdateStatusPermission) throw new NotAllowedError();
const data = formatChangeStatusMessage(
await handleStatusChange(updateOrRevert),
"NEWS",
);
await sendLineNotifyMessage(data);
return { success: true, message: SUCCESS_MESSAGES.UPDATE, data };
} catch (error) {
return await wrapServerSideErrorForClient(error);
}
}