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
8 changes: 4 additions & 4 deletions frontend/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { revalidatePath } from "next/cache";
import { createSupabaseClient } from "@/utils/supabase/server";
import { loginInitialState, requiredOnboardingProgress, signupInitialState } from "@/types/user";
import { createClient } from "@supabase/supabase-js";
import { importQuickbooksData } from "@/api/quickbooks";
import { getCompany } from "@/api/company";
import { getCompanyServerSide } from "@/api/company-server";
import { importQuickbooksDataServerSide } from "../api/quickbooks-server";

export async function login(prevState: loginInitialState, formData: FormData) {
const supabase = await createSupabaseClient();
Expand All @@ -21,9 +21,9 @@ export async function login(prevState: loginInitialState, formData: FormData) {
};
}

const company = await getCompany();
const company = await getCompanyServerSide();
if (company?.externals) {
importQuickbooksData();
importQuickbooksDataServerSide();
}

revalidatePath("/", "layout");
Expand Down
13 changes: 6 additions & 7 deletions frontend/api/business-profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use server";
import { DocumentResponse, PresignedUploadResponse, DocumentCategories, DocumentTypes } from "@/types/documents";
import { authHeader, authWrapper, getClient } from "./client";
import { authHeader, clientAuthWrapper, getClient } from "./client";
import { getCompany } from "./company";
import { gzip } from "pako";

Expand Down Expand Up @@ -28,7 +27,7 @@ export const getAllDocuments = async (): Promise<DocumentResponse[]> => {
return data;
};

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

export async function getBusinessDocumentUploadUrl(
Expand Down Expand Up @@ -56,7 +55,7 @@ export async function getBusinessDocumentUploadUrl(
return data;
};

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

export async function confirmBusinessDocumentUpload(
Expand Down Expand Up @@ -84,7 +83,7 @@ export async function confirmBusinessDocumentUpload(
}
};

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

export async function updateDocumentCategory(documentId: string, category: DocumentCategories): Promise<void> {
Expand All @@ -101,7 +100,7 @@ export async function updateDocumentCategory(documentId: string, category: Docum
}
};

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

export async function deleteBusinessDocument(key: string, documentId: string): Promise<void> {
Expand All @@ -118,7 +117,7 @@ export async function deleteBusinessDocument(key: string, documentId: string): P
}
};

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

export async function uploadToS3(uploadUrl: string, file: File): Promise<void> {
Expand Down
6 changes: 2 additions & 4 deletions frontend/api/claim-location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use server";

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

export const createClaimLocationLink = async (
Expand All @@ -18,5 +16,5 @@ export const createClaimLocationLink = async (
throw Error(error?.error);
}
};
return authWrapper<CreateClaimLocationResponse>()(req);
return clientAuthWrapper<CreateClaimLocationResponse>()(req);
};
25 changes: 12 additions & 13 deletions frontend/api/claim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import {
ConfirmDocumentUploadRequest,
ConfirmDocumentUploadResponse,
Expand All @@ -17,7 +16,7 @@ import {
UploadClaimRelatedDocumentsRequest,
UploadClaimRelatedDocumentsResponse,
} from "@/types/claim";
import { authHeader, authWrapper, getClient } from "./client";
import { authHeader, clientAuthWrapper, getClient } from "./client";

export const createClaim = async (payload: CreateClaimRequest): Promise<CreateClaimResponse> => {
const req = async (token: string): Promise<CreateClaimResponse> => {
Expand All @@ -32,7 +31,7 @@ export const createClaim = async (payload: CreateClaimRequest): Promise<CreateCl
throw Error(error?.error);
}
};
return authWrapper<CreateClaimResponse>()(req);
return clientAuthWrapper<CreateClaimResponse>()(req);
};

export const getClaims = async (input: GetCompanyClaimRequest): Promise<GetCompanyClaimResponse> => {
Expand All @@ -48,7 +47,7 @@ export const getClaims = async (input: GetCompanyClaimRequest): Promise<GetCompa
throw Error(error?.error);
}
};
return authWrapper<GetCompanyClaimResponse>()(req);
return clientAuthWrapper<GetCompanyClaimResponse>()(req);
};

export const getPurchaseLineItemsFromClaim = async (params: {
Expand All @@ -72,7 +71,7 @@ export const getPurchaseLineItemsFromClaim = async (params: {
throw Error(error?.error);
}
};
return authWrapper<GetClaimLineItemsResponse>()(req);
return clientAuthWrapper<GetClaimLineItemsResponse>()(req);
};

export const getClaimById = async (claimId: string): Promise<GetClaimByIdResponse> => {
Expand All @@ -90,7 +89,7 @@ export const getClaimById = async (claimId: string): Promise<GetClaimByIdRespons
throw Error(error?.error);
}
};
return authWrapper<GetClaimByIdResponse>()(req);
return clientAuthWrapper<GetClaimByIdResponse>()(req);
};

export const updateClaimStatus = async (
Expand All @@ -112,7 +111,7 @@ export const updateClaimStatus = async (
throw Error(error?.error);
}
};
return authWrapper<UpdateClaimStatusResponse>()(req);
return clientAuthWrapper<UpdateClaimStatusResponse>()(req);
};

export const uploadAndConfirmDocumentRelation = async (
Expand Down Expand Up @@ -158,7 +157,7 @@ export const uploadClaimRelatedDocuments = async (
throw Error(error?.error);
}
};
return authWrapper<UploadClaimRelatedDocumentsResponse>()(req);
return clientAuthWrapper<UploadClaimRelatedDocumentsResponse>()(req);
};

export const conformUploadedDocument = async (
Expand All @@ -177,7 +176,7 @@ export const conformUploadedDocument = async (
throw Error(error?.error);
}
};
return authWrapper<ConfirmDocumentUploadResponse>()(req);
return clientAuthWrapper<ConfirmDocumentUploadResponse>()(req);
};

export const linkLineItemToClaim = async (claimId: string, purchaseLineItemId: string) => {
Expand All @@ -193,7 +192,7 @@ export const linkLineItemToClaim = async (claimId: string, purchaseLineItemId: s
throw Error(error?.error);
}
};
return authWrapper<LinkLineItemToClaimResponse>()(req);
return clientAuthWrapper<LinkLineItemToClaimResponse>()(req);
};

export const linkPurchaseToClaim = async (claimId: string, purchaseId: string) => {
Expand All @@ -209,7 +208,7 @@ export const linkPurchaseToClaim = async (claimId: string, purchaseId: string) =
throw Error(error?.error);
}
};
return authWrapper<LinkPurchaseToClaimResponse>()(req);
return clientAuthWrapper<LinkPurchaseToClaimResponse>()(req);
};

export const createClaimPDF = async (claimId: string) => {
Expand All @@ -227,7 +226,7 @@ export const createClaimPDF = async (claimId: string) => {
throw Error(error?.error);
}
};
return authWrapper<CreateClaimPDFResponse>()(req);
return clientAuthWrapper<CreateClaimPDFResponse>()(req);
};

export const deleteClaim = async (claimId: string) => {
Expand All @@ -245,5 +244,5 @@ export const deleteClaim = async (claimId: string) => {
throw Error(error?.error);
}
};
return authWrapper<DeleteClaimResponse>()(req);
return clientAuthWrapper<DeleteClaimResponse>()(req);
};
11 changes: 11 additions & 0 deletions frontend/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ export const authWrapper =
export const getClientAuthToken = async (): Promise<string> => {
return await getAuthToken();
};

/**
* Client-side: Wraps a function that needs authentication token
* Uses getAuthToken() which reads from browser cookies/storage
*/
export const clientAuthWrapper =
<T>() =>
async (fn: (token: string) => Promise<T>) => {
const token = await getAuthToken();
return fn(token);
};
19 changes: 19 additions & 0 deletions frontend/api/company-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use server";

import { Company } from "@/types/company";
import { authHeader, authWrapper, getClient } from "./client";

export const getCompanyServerSide = async (): Promise<Company> => {
const req = async (token: string): Promise<Company> => {
const client = getClient();
const { data, error, response } = await client.GET("/companies", {
headers: authHeader(token),
});
if (response.ok) {
return data!;
} else {
throw Error(error?.error);
}
};
return authWrapper<Company>()(req);
};
15 changes: 7 additions & 8 deletions frontend/api/company.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import {
Company,
CompanyHasDataResponse,
Expand All @@ -8,7 +7,7 @@ import {
UpdateCompanyRequest,
UpdateCompanyResponse,
} from "@/types/company";
import { authHeader, authWrapper, getClient } from "./client";
import { authHeader, clientAuthWrapper, getClient } from "./client";

export const createCompany = async (payload: CreateCompanyRequest): Promise<Company> => {
const req = async (token: string): Promise<Company> => {
Expand All @@ -23,7 +22,7 @@ export const createCompany = async (payload: CreateCompanyRequest): Promise<Comp
throw Error(error?.error);
}
};
return authWrapper<Company>()(req);
return clientAuthWrapper<Company>()(req);
};

export const getCompanyLocations = async (): Promise<GetCompanyLocationsResponse> => {
Expand All @@ -38,7 +37,7 @@ export const getCompanyLocations = async (): Promise<GetCompanyLocationsResponse
throw Error(error?.error);
}
};
return authWrapper<GetCompanyLocationsResponse>()(req);
return clientAuthWrapper<GetCompanyLocationsResponse>()(req);
};

export const getCompany = async (): Promise<Company> => {
Expand All @@ -53,7 +52,7 @@ export const getCompany = async (): Promise<Company> => {
throw Error(error?.error);
}
};
return authWrapper<Company>()(req);
return clientAuthWrapper<Company>()(req);
};

export const getClaimInProgress = async (): Promise<GetClaimInProgressForCompanyResponse> => {
Expand All @@ -68,7 +67,7 @@ export const getClaimInProgress = async (): Promise<GetClaimInProgressForCompany
throw Error(error?.error);
}
};
return authWrapper<GetClaimInProgressForCompanyResponse>()(req);
return clientAuthWrapper<GetClaimInProgressForCompanyResponse>()(req);
};

export const companyHasData = async (): Promise<CompanyHasDataResponse> => {
Expand All @@ -83,7 +82,7 @@ export const companyHasData = async (): Promise<CompanyHasDataResponse> => {
throw Error(error?.error);
}
};
return authWrapper<CompanyHasDataResponse>()(req);
return clientAuthWrapper<CompanyHasDataResponse>()(req);
};

export const updateCompany = async (payload: UpdateCompanyRequest): Promise<UpdateCompanyResponse> => {
Expand All @@ -99,5 +98,5 @@ export const updateCompany = async (payload: UpdateCompanyRequest): Promise<Upda
throw Error(error?.error);
}
};
return authWrapper<UpdateCompanyResponse>()(req);
return clientAuthWrapper<UpdateCompanyResponse>()(req);
};
1 change: 0 additions & 1 deletion frontend/api/dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import { BannerData } from "@/types/user";
import { getNotifications } from "./notifications";
import { getClaimInProgress } from "./company";
Expand Down
8 changes: 3 additions & 5 deletions frontend/api/fema-risk-index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use server";

import { FemaRisKIndexCountiesFemaDisaster } from "@/types/fema-risk-index";
import { authHeader, authWrapper, getClient } from "./client";
import { authHeader, clientAuthWrapper, getClient } from "./client";

export const getFemaRiskIndexData = async (): Promise<FemaRisKIndexCountiesFemaDisaster> => {
const req = async (token: string): Promise<FemaRisKIndexCountiesFemaDisaster> => {
Expand All @@ -15,7 +13,7 @@ export const getFemaRiskIndexData = async (): Promise<FemaRisKIndexCountiesFemaD
throw Error(error?.error);
}
};
return authWrapper<FemaRisKIndexCountiesFemaDisaster>()(req);
return clientAuthWrapper<FemaRisKIndexCountiesFemaDisaster>()(req);
};

export const refreshFemaRiskIndexData = async (): Promise<void> => {
Expand All @@ -28,5 +26,5 @@ export const refreshFemaRiskIndexData = async (): Promise<void> => {
throw Error(error?.error);
}
};
return authWrapper<void>()(req);
return clientAuthWrapper<void>()(req);
};
15 changes: 7 additions & 8 deletions frontend/api/insurance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use server";
import {
CreateInsurancePolicyBulkRequest,
CreateInsurancePolicyRequest,
Expand All @@ -9,7 +8,7 @@ import {
UpdateInsurancePolicyRequest,
UpdateInsurancePolicyResponse,
} from "@/types/insurance-policy";
import { getClient, authHeader, authWrapper } from "./client";
import { getClient, authHeader, clientAuthWrapper } from "./client";

export const createInsurancePolicy = async (payload: CreateInsurancePolicyRequest): Promise<InsurancePolicy> => {
const req = async (token: string): Promise<InsurancePolicy> => {
Expand All @@ -24,7 +23,7 @@ export const createInsurancePolicy = async (payload: CreateInsurancePolicyReques
throw Error(error?.error);
}
};
return authWrapper<InsurancePolicy>()(req);
return clientAuthWrapper<InsurancePolicy>()(req);
};

export const createInsurancePolicyBulk = async (
Expand All @@ -42,7 +41,7 @@ export const createInsurancePolicyBulk = async (
throw Error(error?.error);
}
};
return authWrapper<InsurancePolicy[]>()(req);
return clientAuthWrapper<InsurancePolicy[]>()(req);
};

export const getInsurancePolicies = async (): Promise<GetInsurancePoliciesResponseType> => {
Expand All @@ -57,7 +56,7 @@ export const getInsurancePolicies = async (): Promise<GetInsurancePoliciesRespon
throw Error(error?.error);
}
};
return authWrapper<GetInsurancePoliciesResponseType>()(req);
return clientAuthWrapper<GetInsurancePoliciesResponseType>()(req);
};

export const updateInsurancePolicy = async (
Expand All @@ -75,7 +74,7 @@ export const updateInsurancePolicy = async (
throw Error(error?.error);
}
};
return authWrapper<UpdateInsurancePolicyResponse>()(req);
return clientAuthWrapper<UpdateInsurancePolicyResponse>()(req);
};

export const updateInsurancePolicyBulk = async (
Expand All @@ -93,7 +92,7 @@ export const updateInsurancePolicyBulk = async (
throw Error(error?.error);
}
};
return authWrapper<UpdateInsurancePolicyBulkResponse>()(req);
return clientAuthWrapper<UpdateInsurancePolicyBulkResponse>()(req);
};

export const deleteInsurancePolicy = async (insurancePolicyId: string): Promise<void> => {
Expand All @@ -111,5 +110,5 @@ export const deleteInsurancePolicy = async (insurancePolicyId: string): Promise<
throw Error(error?.error);
}
};
return authWrapper<void>()(req);
return clientAuthWrapper<void>()(req);
};
Loading
Loading