Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion frontend/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export async function login(prevState: loginInitialState, formData: FormData) {
}

const company = await getCompany();
if (company?.externals) {
if ("error" in company) {
console.error("Error getting company:", company.error);
return {
success: false,
message: company.error || "Login failed",
};
}
const companyData = company.data;
if (companyData?.externals) {
importQuickbooksData();
}

Expand Down
78 changes: 51 additions & 27 deletions frontend/api/business-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { DocumentResponse, PresignedUploadResponse, DocumentCategories, Document
import { authHeader, authWrapper, getClient } from "./client";
import { getCompany } from "./company";
import { gzip } from "pako";
import { ServerActionResult, isServerActionError } from "./types";

export const getAllDocuments = async (): Promise<DocumentResponse[]> => {
const req = async (token: string): Promise<DocumentResponse[]> => {
const companyId = (await getCompany()).id;
export const getAllDocuments = async (): Promise<ServerActionResult<DocumentResponse[]>> => {
const req = async (token: string): Promise<ServerActionResult<DocumentResponse[]>> => {
const companyResult = await getCompany();
if (isServerActionError(companyResult)) {
return { success: false, error: companyResult.error };
}
const companyId = companyResult.data.id;
const documentType = DocumentTypes.GENERAL_BUSINESS;

const client = getClient();
Expand All @@ -22,21 +27,25 @@ export const getAllDocuments = async (): Promise<DocumentResponse[]> => {
});

if (!response.ok || !data) {
throw new Error(error?.error || "Failed to fetch documents");
return { success: false, error: error?.error || "Failed to fetch documents" };
}

return data;
return { success: true, data };
};

return authWrapper<DocumentResponse[]>()(req);
return authWrapper<ServerActionResult<DocumentResponse[]>>()(req);
};

export async function getBusinessDocumentUploadUrl(
fileName: string,
fileType: string
): Promise<PresignedUploadResponse> {
const req = async (token: string): Promise<PresignedUploadResponse> => {
const companyId = (await getCompany()).id;
): Promise<ServerActionResult<PresignedUploadResponse>> {
const req = async (token: string): Promise<ServerActionResult<PresignedUploadResponse>> => {
const companyResult = await getCompany();
if (isServerActionError(companyResult)) {
return { success: false, error: companyResult.error };
}
const companyId = companyResult.data.id;
const client = getClient();

const { data, error, response } = await client.POST("/s3/getUploadUrl", {
Expand All @@ -50,23 +59,27 @@ export async function getBusinessDocumentUploadUrl(
});

if (!response.ok || !data) {
throw new Error(error?.error || "Failed to get upload URL");
return { success: false, error: error?.error || "Failed to get upload URL" };
}

return data;
return { success: true, data };
};

return authWrapper<PresignedUploadResponse>()(req);
return authWrapper<ServerActionResult<PresignedUploadResponse>>()(req);
}

export async function confirmBusinessDocumentUpload(
key: string,
documentId: string,
category?: DocumentCategories
): Promise<void> {
const req = async (token: string): Promise<void> => {
): Promise<ServerActionResult<void>> {
const req = async (token: string): Promise<ServerActionResult<void>> => {
const client = getClient();
const companyId = (await getCompany()).id;
const companyResult = await getCompany();
if (isServerActionError(companyResult)) {
return { success: false, error: companyResult.error };
}
const companyId = companyResult.data.id;

const { error, response } = await client.POST("/s3/confirmUpload", {
headers: authHeader(token),
Expand All @@ -80,15 +93,20 @@ export async function confirmBusinessDocumentUpload(
});

if (!response.ok) {
throw new Error(error?.error || "Failed to confirm upload");
return { success: false, error: error?.error || "Failed to confirm upload" };
}

return { success: true, data: undefined };
};

return authWrapper<void>()(req);
return authWrapper<ServerActionResult<void>>()(req);
}

export async function updateDocumentCategory(documentId: string, category: DocumentCategories): Promise<void> {
const req = async (token: string): Promise<void> => {
export async function updateDocumentCategory(
documentId: string,
category: DocumentCategories
): Promise<ServerActionResult<void>> {
const req = async (token: string): Promise<ServerActionResult<void>> => {
const client = getClient();

const { error, response } = await client.PATCH("/s3/updateDocumentCategory", {
Expand All @@ -97,15 +115,17 @@ export async function updateDocumentCategory(documentId: string, category: Docum
});

if (!response.ok) {
throw new Error(error?.error || "Failed to update category");
return { success: false, error: error?.error || "Failed to update category" };
}

return { success: true, data: undefined };
};

return authWrapper<void>()(req);
return authWrapper<ServerActionResult<void>>()(req);
}

export async function deleteBusinessDocument(key: string, documentId: string): Promise<void> {
const req = async (token: string): Promise<void> => {
export async function deleteBusinessDocument(key: string, documentId: string): Promise<ServerActionResult<void>> {
const req = async (token: string): Promise<ServerActionResult<void>> => {
const client = getClient();

const { error, response } = await client.DELETE("/s3/deleteDocument", {
Expand All @@ -114,14 +134,16 @@ export async function deleteBusinessDocument(key: string, documentId: string): P
});

if (!response.ok) {
throw new Error(error?.error || "Failed to delete document");
return { success: false, error: error?.error || "Failed to delete document" };
}

return { success: true, data: undefined };
};

return authWrapper<void>()(req);
return authWrapper<ServerActionResult<void>>()(req);
}

export async function uploadToS3(uploadUrl: string, file: File): Promise<void> {
export async function uploadToS3(uploadUrl: string, file: File): Promise<ServerActionResult<void>> {
const arrayBuffer = await file.arrayBuffer();
const compressed = gzip(new Uint8Array(arrayBuffer));

Expand All @@ -135,6 +157,8 @@ export async function uploadToS3(uploadUrl: string, file: File): Promise<void> {
});

if (!response.ok) {
throw new Error(`Upload failed: ${response.statusText}`);
return { success: false, error: `Upload failed: ${response.statusText}` };
}

return { success: true, data: undefined };
}
11 changes: 6 additions & 5 deletions frontend/api/claim-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import { authHeader, authWrapper, getClient } from "./client";
import { CreateClaimLocationRequest, CreateClaimLocationResponse } from "@/types/claim-location";
import { ServerActionResult } from "./types";

export const createClaimLocationLink = async (
payload: CreateClaimLocationRequest
): Promise<CreateClaimLocationResponse> => {
const req = async (token: string): Promise<CreateClaimLocationResponse> => {
): Promise<ServerActionResult<CreateClaimLocationResponse>> => {
const req = async (token: string): Promise<ServerActionResult<CreateClaimLocationResponse>> => {
const client = getClient();
const { data, error, response } = await client.POST("/claim-locations", {
headers: authHeader(token),
body: payload,
});
if (response.ok) {
return data!;
return { success: true, data: data! };
} else {
throw Error(error?.error);
return { success: false, error: error?.error || "Failed to create claim location link" };
}
};
return authWrapper<CreateClaimLocationResponse>()(req);
return authWrapper<ServerActionResult<CreateClaimLocationResponse>>()(req);
};
Loading
Loading