first commit
This commit is contained in:
256
frontend/src/lib/calendar.ts
Normal file
256
frontend/src/lib/calendar.ts
Normal file
@@ -0,0 +1,256 @@
|
||||
import { API_BASE_URL, apiFormRequest, apiRequest } from "@/lib/api";
|
||||
|
||||
export type Holiday = {
|
||||
id: string;
|
||||
name: string;
|
||||
date: string;
|
||||
type: string;
|
||||
source: string;
|
||||
};
|
||||
|
||||
export type CustomDate = {
|
||||
id: string;
|
||||
client_id: string | null;
|
||||
name: string;
|
||||
description: string;
|
||||
date: string;
|
||||
recurs_annually: boolean;
|
||||
visibility: "agency" | "client";
|
||||
category: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
|
||||
export type CalendarItem = {
|
||||
id: string;
|
||||
client_id: string;
|
||||
client_name: string;
|
||||
title: string;
|
||||
description: string;
|
||||
content_type: string;
|
||||
status: "rascunho" | "planejado" | "em_producao" | "em_revisao" | "aprovado" | "publicado" | "cancelado";
|
||||
owner_id: string | null;
|
||||
scheduled_date: string;
|
||||
scheduled_at: string | null;
|
||||
copy_text: string;
|
||||
internal_notes: string;
|
||||
client_notes: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
|
||||
export type Attachment = {
|
||||
id: string;
|
||||
calendar_item_id: string;
|
||||
original_filename: string;
|
||||
stored_filename: string;
|
||||
mime_type: string;
|
||||
size_bytes: number;
|
||||
storage_driver: string;
|
||||
created_by: string | null;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type CreateCustomDateInput = {
|
||||
client_id?: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
date: string;
|
||||
recurs_annually: boolean;
|
||||
visibility: "agency" | "client";
|
||||
category: string;
|
||||
};
|
||||
|
||||
export type UpdateCustomDateInput = {
|
||||
client_id?: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
date: string;
|
||||
recurs_annually: boolean;
|
||||
visibility: "agency" | "client";
|
||||
};
|
||||
|
||||
export type CreateCalendarItemInput = {
|
||||
client_id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
content_type: string;
|
||||
status: CalendarItem["status"];
|
||||
scheduled_date: string;
|
||||
copy_text?: string;
|
||||
internal_notes?: string;
|
||||
client_notes?: string;
|
||||
};
|
||||
|
||||
export type UpdateCalendarItemInput = Partial<{
|
||||
title: string;
|
||||
description: string;
|
||||
status: CalendarItem["status"];
|
||||
scheduled_date: string;
|
||||
copy_text: string;
|
||||
internal_notes: string;
|
||||
client_notes: string;
|
||||
}>;
|
||||
|
||||
type HolidaysResponse = {
|
||||
holidays: Holiday[];
|
||||
};
|
||||
|
||||
type CustomDatesResponse = {
|
||||
custom_dates: CustomDate[];
|
||||
};
|
||||
|
||||
type CustomDateResponse = {
|
||||
custom_date: CustomDate;
|
||||
};
|
||||
|
||||
type CalendarItemsResponse = {
|
||||
items: CalendarItem[];
|
||||
};
|
||||
|
||||
type CalendarItemResponse = {
|
||||
item: CalendarItem;
|
||||
};
|
||||
|
||||
type AttachmentsResponse = {
|
||||
attachments: Attachment[];
|
||||
};
|
||||
|
||||
type AttachmentResponse = {
|
||||
attachment: Attachment;
|
||||
};
|
||||
|
||||
export async function listHolidays(token: string, year: number) {
|
||||
const response = await apiRequest<HolidaysResponse>(`/calendar/holidays?year=${year}`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.holidays;
|
||||
}
|
||||
|
||||
export async function listCustomDates(token: string, year: number, clientId?: string | null) {
|
||||
const params = new URLSearchParams({ year: String(year) });
|
||||
if (clientId) {
|
||||
params.set("client_id", clientId);
|
||||
}
|
||||
|
||||
const response = await apiRequest<CustomDatesResponse>(`/calendar/custom-dates?${params.toString()}`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.custom_dates;
|
||||
}
|
||||
|
||||
export async function createCustomDate(token: string, input: CreateCustomDateInput) {
|
||||
const response = await apiRequest<CustomDateResponse>("/calendar/custom-dates", {
|
||||
method: "POST",
|
||||
token,
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
return response.custom_date;
|
||||
}
|
||||
|
||||
export async function updateCustomDate(token: string, customDateId: string, input: UpdateCustomDateInput) {
|
||||
const response = await apiRequest<CustomDateResponse>(`/calendar/custom-dates/${customDateId}`, {
|
||||
method: "PATCH",
|
||||
token,
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
return response.custom_date;
|
||||
}
|
||||
|
||||
export async function deleteCustomDate(token: string, customDateId: string) {
|
||||
await apiRequest<{ message: string }>(`/calendar/custom-dates/${customDateId}`, {
|
||||
method: "DELETE",
|
||||
token,
|
||||
});
|
||||
}
|
||||
|
||||
export async function listCalendarItems(token: string, year: number, clientId?: string | null) {
|
||||
const params = new URLSearchParams({ year: String(year) });
|
||||
if (clientId) {
|
||||
params.set("client_id", clientId);
|
||||
}
|
||||
|
||||
const response = await apiRequest<CalendarItemsResponse>(`/calendar/items?${params.toString()}`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.items;
|
||||
}
|
||||
|
||||
export async function listCalendarItemsByRange(token: string, from: string, to: string, clientId?: string | null) {
|
||||
const params = new URLSearchParams({ from, to });
|
||||
if (clientId) {
|
||||
params.set("client_id", clientId);
|
||||
}
|
||||
|
||||
const response = await apiRequest<CalendarItemsResponse>(`/calendar/items?${params.toString()}`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.items;
|
||||
}
|
||||
|
||||
export async function createCalendarItem(token: string, input: CreateCalendarItemInput) {
|
||||
const response = await apiRequest<CalendarItemResponse>("/calendar/items", {
|
||||
method: "POST",
|
||||
token,
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
return response.item;
|
||||
}
|
||||
|
||||
export async function getCalendarItem(token: string, itemId: string) {
|
||||
const response = await apiRequest<CalendarItemResponse>(`/calendar/items/${itemId}`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.item;
|
||||
}
|
||||
|
||||
export async function updateCalendarItem(token: string, itemId: string, input: UpdateCalendarItemInput) {
|
||||
const response = await apiRequest<CalendarItemResponse>(`/calendar/items/${itemId}`, {
|
||||
method: "PATCH",
|
||||
token,
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
return response.item;
|
||||
}
|
||||
|
||||
export async function cancelCalendarItem(token: string, itemId: string) {
|
||||
await apiRequest<{ message: string }>(`/calendar/items/${itemId}`, {
|
||||
method: "DELETE",
|
||||
token,
|
||||
});
|
||||
}
|
||||
|
||||
export async function listAttachments(token: string, itemId: string) {
|
||||
const response = await apiRequest<AttachmentsResponse>(`/calendar/items/${itemId}/attachments`, {
|
||||
method: "GET",
|
||||
token,
|
||||
});
|
||||
|
||||
return response.attachments;
|
||||
}
|
||||
|
||||
export async function uploadAttachment(token: string, itemId: string, file: File) {
|
||||
const formData = new FormData();
|
||||
formData.set("file", file);
|
||||
|
||||
const response = await apiFormRequest<AttachmentResponse>(`/calendar/items/${itemId}/attachments`, formData, token);
|
||||
return response.attachment;
|
||||
}
|
||||
|
||||
export function attachmentDownloadURL(attachmentId: string) {
|
||||
return `${API_BASE_URL}/calendar/attachments/${attachmentId}/download`;
|
||||
}
|
||||
Reference in New Issue
Block a user