Skip to content

Commit

Permalink
feat(finance/billing): add new and update forms for billing page (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusualhashash authored Nov 15, 2024
2 parents 84a64db + 49728df commit e546225
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"use client";

import { toast } from "@/components/ui/sonner";
import type {
UniRefund_FinanceService_Billings_BillingDto,
UniRefund_FinanceService_Billings_UpdateBillingDto,
} from "@ayasofyazilim/saas/FinanceService";
import { $UniRefund_FinanceService_Billings_UpdateBillingDto } from "@ayasofyazilim/saas/FinanceService";
import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object";
import AutoForm, {
AutoFormSubmit,
createFieldConfigWithResource,
} from "@repo/ayasofyazilim-ui/organisms/auto-form";
import { putBillingApi } from "src/app/[lang]/app/actions/FinanceService/put-actions";
import type { FinanceServiceResource } from "src/language-data/FinanceService";

const updateBillingSchema = createZodObject(
$UniRefund_FinanceService_Billings_UpdateBillingDto,
);

export default function Form({
billingId,
languageData,
billingData,
}: {
billingId: string;
languageData: FinanceServiceResource;
billingData: UniRefund_FinanceService_Billings_BillingDto;
}) {
async function updateBilling(
data: UniRefund_FinanceService_Billings_UpdateBillingDto,
) {
const response = await putBillingApi({
id: billingId,
requestBody: data,
});
if (response.type === "success") {
toast.success(languageData["Billing.Update.Success"]);
} else {
toast.error(response.type + response.message || ["Billing.Update.Fail"]);
}
}

const translatedForm = createFieldConfigWithResource({
schema: $UniRefund_FinanceService_Billings_UpdateBillingDto,
resources: languageData,
});

return (
<AutoForm
fieldConfig={translatedForm}
formSchema={updateBillingSchema}
onSubmit={(formdata) => {
void updateBilling(
formdata as UniRefund_FinanceService_Billings_UpdateBillingDto,
);
}}
values={billingData}
>
<AutoFormSubmit className="float-right">
{languageData["Edit.Save"]}
</AutoFormSubmit>
</AutoForm>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use server";
import { getBillingDetailApi } from "src/app/[lang]/app/actions/FinanceService/actions";
import { getResourceData } from "src/language-data/FinanceService";
import Form from "./form";

export default async function Page({
params,
}: {
params: { lang: string; billingId: string };
}) {
const { languageData } = await getResourceData(params.lang);
const billing = await getBillingDetailApi(params.billingId);

if (billing.type !== "success") {
return (
<div className="error-message">
{billing.type + billing.message || languageData["Billing.Fetch.Fail"]}
</div>
);
}

const billingList = billing.data;

return (
<Form
billingData={billingList}
billingId={params.billingId}
languageData={languageData}
/>
);
}
95 changes: 95 additions & 0 deletions apps/web/src/app/[lang]/app/[type]/finance/billing/new/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"use client";
import { toast } from "@/components/ui/sonner";
import type { UniRefund_CRMService_Merchants_MerchantProfileDto } from "@ayasofyazilim/saas/CRMService";
import type { UniRefund_FinanceService_Billings_CreateBillingDto } from "@ayasofyazilim/saas/FinanceService";
import { $UniRefund_FinanceService_Billings_CreateBillingDto } from "@ayasofyazilim/saas/FinanceService";
import { createZodObject } from "@repo/ayasofyazilim-ui/lib/create-zod-object";
import AutoForm, {
AutoFormSubmit,
createFieldConfigWithResource,
CustomCombobox,
} from "@repo/ayasofyazilim-ui/organisms/auto-form";
import { useRouter } from "next/navigation";
import { postBillingApi } from "src/app/[lang]/app/actions/FinanceService/post-actions";
import type { CRMServiceServiceResource } from "src/language-data/CRMService";
import type { FinanceServiceResource } from "src/language-data/FinanceService";
import { getBaseLink } from "src/utils";

const billingSchema = createZodObject(
$UniRefund_FinanceService_Billings_CreateBillingDto,
);

export default function Page({
languageData,
merchants,
}: {
languageData: {
crm: CRMServiceServiceResource;
finance: FinanceServiceResource;
};
merchants: {
success: boolean;
data: UniRefund_CRMService_Merchants_MerchantProfileDto[];
};
}) {
const router = useRouter();

async function createBilling(
data: UniRefund_FinanceService_Billings_CreateBillingDto,
) {
const response = await postBillingApi({ requestBody: data });
if (response.type === "error" || response.type === "api-error") {
toast.error(
response.type +
(response.message || languageData.finance["Billing.New.Error"]),
);
} else {
toast.success([languageData.finance["Billing.New.Success"]]);
router.push(getBaseLink(`/app/admin/finance/billing`));
}
}

const translatedForm = createFieldConfigWithResource({
schema: $UniRefund_FinanceService_Billings_CreateBillingDto,
resources: languageData.finance,
extend: {
merchantId: {
renderer: (props) => {
return (
<CustomCombobox<UniRefund_CRMService_Merchants_MerchantProfileDto>
childrenProps={props}
disabled={!merchants.success}
emptyValue={
merchants.success
? languageData.crm["Merchant.Select"]
: languageData.crm["Merchants.Fetch.Fail"]
}
list={merchants.data}
searchPlaceholder={languageData.finance["Select.Placeholder"]}
searchResultLabel={languageData.finance["Select.ResultLabel"]}
selectIdentifier="id"
selectLabel="name"
/>
);
},
},
},
});

return (
<AutoForm
fieldConfig={translatedForm}
formSchema={billingSchema}
onSubmit={(val) => {
void createBilling(
val as UniRefund_FinanceService_Billings_CreateBillingDto,
);
}}
stickyChildren
>
<AutoFormSubmit className="float-right px-8 py-4">
{languageData.finance.Save}
</AutoFormSubmit>
</AutoForm>
);
}
29 changes: 29 additions & 0 deletions apps/web/src/app/[lang]/app/[type]/finance/billing/new/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { getMerchantsApi } from "src/app/[lang]/app/actions/CrmService/actions";
import { getResourceData as getFinanceResources } from "src/language-data/FinanceService";
import { getResourceData as getCRMResources } from "src/language-data/CRMService";
import Form from "./form";

export default async function Page({ params }: { params: { lang: string } }) {
const { languageData: financeLanguageData } = await getFinanceResources(
params.lang,
);
const { languageData: crmLanguageData } = await getCRMResources(params.lang);
const merchant = await getMerchantsApi();
const merchantsList =
(merchant.type === "success" && merchant.data.items) || [];

return (
<Form
languageData={{
finance: financeLanguageData,
crm: crmLanguageData,
}}
merchants={{
data: merchantsList,
success: merchant.type === "success",
}}
/>
);
}
5 changes: 2 additions & 3 deletions apps/web/src/app/[lang]/app/actions/FinanceService/actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use server";
import type { GetApiFinanceServiceBillingsData } from "@ayasofyazilim/saas/FinanceService";
import { structuredError, structuredResponse } from "src/lib";
import { getApiRequests } from "../api-requests";

export async function getBillingApi(data: GetApiFinanceServiceBillingsData) {
export async function getBillingDetailApi(id: string) {
try {
const requests = await getApiRequests();
const dataResponse = await requests.billing.get(data);
const dataResponse = await requests.billing.getDetail(id);
return structuredResponse(dataResponse);
} catch (error) {
return structuredError(error);
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/app/[lang]/app/actions/FinanceService/post-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use server";
import type { PostApiFinanceServiceBillingsData } from "@ayasofyazilim/saas/FinanceService";
import { structuredError, structuredResponse } from "src/lib";
import { getApiRequests } from "../api-requests";

export async function postBillingApi(data: PostApiFinanceServiceBillingsData) {
try {
const requests = await getApiRequests();
const dataResponse = await requests.billing.post(data);
return structuredResponse(dataResponse);
} catch (error) {
return structuredError(error);
}
}
16 changes: 16 additions & 0 deletions apps/web/src/app/[lang]/app/actions/FinanceService/put-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use server";
import type { PutApiFinanceServiceBillingsByIdData } from "@ayasofyazilim/saas/FinanceService";
import { structuredError, structuredResponse } from "src/lib";
import { getApiRequests } from "../api-requests";

export async function putBillingApi(
data: PutApiFinanceServiceBillingsByIdData,
) {
try {
const requests = await getApiRequests();
const dataResponse = await requests.billing.put(data);
return structuredResponse(dataResponse);
} catch (error) {
return structuredError(error);
}
}
14 changes: 13 additions & 1 deletion apps/web/src/app/[lang]/app/actions/api-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ import type {
PostApiExportValidationServiceExportValidationData,
PutApiExportValidationServiceExportValidationByIdData,
} from "@ayasofyazilim/saas/ExportValidationService";
import type { GetApiFinanceServiceBillingsData } from "@ayasofyazilim/saas/FinanceService";
import type {
GetApiFinanceServiceBillingsData,
PostApiFinanceServiceBillingsData,
PutApiFinanceServiceBillingsByIdData,
} from "@ayasofyazilim/saas/FinanceService";
import type {
GetApiIdentityClaimTypesData,
GetApiIdentityRolesByIdClaimsData,
Expand Down Expand Up @@ -603,6 +607,14 @@ export async function getApiRequests() {
billing: {
get: async (data: GetApiFinanceServiceBillingsData) =>
await financeClient.billing.getApiFinanceServiceBillings(data),
getDetail: async (id: string) =>
await financeClient.billing.getApiFinanceServiceBillingsById({
id,
}),
post: async (data: PostApiFinanceServiceBillingsData) =>
await financeClient.billing.postApiFinanceServiceBillings(data),
put: async (data: PutApiFinanceServiceBillingsByIdData) =>
await financeClient.billing.putApiFinanceServiceBillingsById(data),
deleteRow: async (id: string) =>
await financeClient.billing.deleteApiFinanceServiceBillings({
id,
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/language-data/CRMService/resources/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Merchants": "Merchants",
"Merchants.Description": "You can view stores from here.",
"Merchant.Select": "Select Merchant",

"Merchants.New": "New Merchant",
"Merchants.New.Description": "You can create a new store from here.",
Expand All @@ -12,6 +13,9 @@
"Merchants.Edit.Success": "Store updated successfully.",
"Merchants.Edit.Fail": "An error occurred while updating the store.",

"Merchants.Fetch.Success": "Merchants fetched successfully.",
"Merchants.Fetch.Fail": "An error occurred while fetching merchants.",

"Merchants.SubOrganization": "Sub Stores",
"Merchants.SubOrganization.Description": "You can view sub stores related to the main store from here.",
"Merchants.SubOrganization.New": "New Sub Store",
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/language-data/CRMService/resources/tr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Merchants": "Mağazalar",
"Merchants.Description": "Mağazaları buradan görüntüleyebilirsiniz.",
"Merchant.Select": "Mağaza seç",

"Merchants.New": "Yeni Mağaza",
"Merchants.New.Description": "Buradan yeni mağaza oluşturabilirsiniz.",
Expand All @@ -12,6 +13,9 @@
"Merchants.Edit.Success": "Mağaza başarıyla güncellendi.",
"Merchants.Edit.Fail": "Mağaza güncellenirken bir hata oluştu.",

"Merchants.Fetch.Success": "Mağazalar basarıyla getirildi.",
"Merchants.Fetch.Fail": "Mağazalar getirilirken bir hata oluştu.",

"Merchants.SubOrganization": "Alt Mağazalar",
"Merchants.SubOrganization.Description": "Buradan mağazaya bağlı alt mağazaları görüntüleyebilirsiniz.",
"Merchants.SubOrganization.New": "Yeni Alt Mağaza",
Expand Down
17 changes: 16 additions & 1 deletion apps/web/src/language-data/FinanceService/resources/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
{
"Billing.New": "New Billing"
"Billing.New": "New Billing",
"Billing.New.Error": "Failed to add Billing",
"Billing.New.Success": " Billing Added Successfully",
"Billing.Update.Success": "Billing Updated Successfully",
"Billing.Update.Fail": "Failed to update Billing",
"Billing.Fetch.Fail": "An error occurred while fetching the billing data. Please try again later.",

"Form.merchantId": "Merchant Name",
"Form.date": "Date",
"Form.number": "Number",
"Form.dueDate": "Due Date",
"Form.total": "Total",
"Form.unpaid": "Unpaid",
"Form.status": "Status",
"Form.period": "Period",
"Form.paymentStatus": "Payment Status"
}
17 changes: 16 additions & 1 deletion apps/web/src/language-data/FinanceService/resources/tr.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
{
"Billing.New": "Yeni fatura"
"Billing.New": "Yeni fatura",
"Billing.New.Error": "Fatura eklenirken bir hata oluştu",
"Billing.New.Success": "Fatura başarıyla eklendi",
"Billing.Update.Success": "Fatura basarıyla guncellendi",
"Billing.Update.Fail": "Fatura guncellenirken bir hata olustu",
"Billing.Fetch.Fail": "Fatura verileri getirilirken bir hata oluştu. Lütfen daha sonra tekrar deneyin.",

"Form.merchantId": "Mağaza Adı",
"Form.date": "Tarih",
"Form.number": "Numara",
"Form.dueDate": "Vade Tarihi",
"Form.total": "Toplam",
"Form.unpaid": "Ödenmemiş",
"Form.status": "Durum",
"Form.period": "Dönem",
"Form.paymentStatus": "Ödeme Durumu"
}

0 comments on commit e546225

Please sign in to comment.