|
| 1 | +import axiosInstance from "@/config/axios-config"; |
| 2 | + |
| 3 | +export interface GroupAccumulatorImage { |
| 4 | + thumbnail: string; |
| 5 | + medium: string; |
| 6 | + original: string; |
| 7 | +} |
| 8 | + |
| 9 | +export interface GroupAccumulatorDTO { |
| 10 | + id: string; |
| 11 | + accumulator_id: string | null; |
| 12 | + group_id: string; |
| 13 | + title: string | null; |
| 14 | + image: GroupAccumulatorImage | null; |
| 15 | + image_key: string | null; |
| 16 | + target_count: number | null; |
| 17 | + start_date: string | null; |
| 18 | + end_date: string | null; |
| 19 | + created_at: string; |
| 20 | + updated_at: string | null; |
| 21 | +} |
| 22 | + |
| 23 | +export interface GroupAccumulatorsResponse { |
| 24 | + accumulators: GroupAccumulatorDTO[]; |
| 25 | + total: number; |
| 26 | + skip: number; |
| 27 | + limit: number; |
| 28 | +} |
| 29 | + |
| 30 | +export interface CreateGroupAccumulatorRequest { |
| 31 | + accumulator_id?: string | null; |
| 32 | + title?: string | null; |
| 33 | + image_key?: string | null; |
| 34 | + target_count?: number | null; |
| 35 | + start_date?: string | null; |
| 36 | + end_date?: string | null; |
| 37 | +} |
| 38 | + |
| 39 | +export type UpdateGroupAccumulatorRequest = CreateGroupAccumulatorRequest; |
| 40 | + |
| 41 | +const getAuthHeaders = () => ({ |
| 42 | + Authorization: `Bearer ${sessionStorage.getItem("accessToken")}`, |
| 43 | +}); |
| 44 | + |
| 45 | +export function resolveGroupAccumulatorImageUrl( |
| 46 | + accumulator: Pick<GroupAccumulatorDTO, "image">, |
| 47 | +): string | null { |
| 48 | + const image = accumulator.image; |
| 49 | + if (!image) return null; |
| 50 | + return image.medium || image.thumbnail || image.original || null; |
| 51 | +} |
| 52 | + |
| 53 | +export const fetchGroupAccumulators = async ( |
| 54 | + groupId: string, |
| 55 | + params?: { skip?: number; limit?: number }, |
| 56 | +): Promise<GroupAccumulatorsResponse> => { |
| 57 | + const { data } = await axiosInstance.get<GroupAccumulatorsResponse>( |
| 58 | + `/api/v1/cms/groups/${groupId}/accumulators`, |
| 59 | + { |
| 60 | + headers: getAuthHeaders(), |
| 61 | + params: { |
| 62 | + skip: params?.skip ?? 0, |
| 63 | + limit: params?.limit ?? 100, |
| 64 | + }, |
| 65 | + }, |
| 66 | + ); |
| 67 | + return data; |
| 68 | +}; |
| 69 | + |
| 70 | +export const createGroupAccumulator = async ( |
| 71 | + groupId: string, |
| 72 | + payload: CreateGroupAccumulatorRequest, |
| 73 | +): Promise<GroupAccumulatorDTO> => { |
| 74 | + const { data } = await axiosInstance.post<GroupAccumulatorDTO>( |
| 75 | + `/api/v1/cms/groups/${groupId}/accumulators`, |
| 76 | + payload, |
| 77 | + { headers: getAuthHeaders() }, |
| 78 | + ); |
| 79 | + return data; |
| 80 | +}; |
| 81 | + |
| 82 | +export const updateGroupAccumulator = async ( |
| 83 | + groupId: string, |
| 84 | + groupAccumulatorId: string, |
| 85 | + payload: UpdateGroupAccumulatorRequest, |
| 86 | +): Promise<GroupAccumulatorDTO> => { |
| 87 | + const { data } = await axiosInstance.put<GroupAccumulatorDTO>( |
| 88 | + `/api/v1/cms/groups/${groupId}/accumulators/${groupAccumulatorId}`, |
| 89 | + payload, |
| 90 | + { headers: getAuthHeaders() }, |
| 91 | + ); |
| 92 | + return data; |
| 93 | +}; |
| 94 | + |
| 95 | +export const deleteGroupAccumulator = async ( |
| 96 | + groupId: string, |
| 97 | + groupAccumulatorId: string, |
| 98 | +): Promise<void> => { |
| 99 | + await axiosInstance.delete( |
| 100 | + `/api/v1/cms/groups/${groupId}/accumulators/${groupAccumulatorId}`, |
| 101 | + { headers: getAuthHeaders() }, |
| 102 | + ); |
| 103 | +}; |
0 commit comments