diff --git a/apps/web/src/app/[lang]/app/[type]/company/[data]/page.tsx b/apps/web/src/app/[lang]/app/[type]/company/[data]/page.tsx index 4861aa9cc..f5cb6cb24 100644 --- a/apps/web/src/app/[lang]/app/[type]/company/[data]/page.tsx +++ b/apps/web/src/app/[lang]/app/[type]/company/[data]/page.tsx @@ -22,7 +22,6 @@ import { $showRefund_points, $showTax_free, } from "./schemas.gen"; - async function controlledFetch( url: string, options: RequestInit, @@ -44,7 +43,6 @@ async function controlledFetch( toast.error("Something went wrong 3 "); } } - interface formModifier { formPositions?: string[]; excludeList?: string[]; @@ -57,7 +55,6 @@ interface formModifier { when: (_value: any) => boolean; }[]; } - interface tableData { createFormSchema: formModifier; editFormSchema: formModifier; @@ -67,21 +64,107 @@ interface tableData { const dataConfig: Record = { merchants: { - filterBy: "Company", + filterBy: "name", createFormSchema: { - formPositions: ["Company", "CustomerNumber", "ProductGroups", "Address"], + formPositions: [ + "name", + "taxpayerId", + "legalStatusCode", + "customerNumber", + "areaCode", + "localNumber", + "ituCountryCode", + "primaryFlag", + "telephoneTypeCode", + "addressLine", + "city", + "terriority", + "postalCode", + "country", + "fullAddress", + "addressPrimaryFlag", + "addressTypeCode", + "emailAddress", + "emailPrimaryFlag", + "emailTypeCode", + "productName", + "vatRate", + "productCode", + "isActive", + ], + schema: $createMerchants, }, editFormSchema: { - formPositions: ["Company", "CustomerNumber", "ProductGroups", "Address"], + formPositions: [ + "name", + "taxpayerId", + "legalStatusCode", + "customerNumber", + "areaCode", + "localNumber", + "ituCountryCode", + "primaryFlag", + "telephoneTypeCode", + "addressLine", + "city", + "terriority", + "postalCode", + "country", + "fullAddress", + "addressPrimaryFlag", + "addressTypeCode", + "emailAddress", + "emailPrimaryFlag", + "emailTypeCode", + "productName", + "vatRate", + "productCode", + "isActive", + ], schema: $editMerchants, }, tableSchema: { - formPositions: ["Company", "CustomerNumber", "ProductGroups", "Address"], + excludeList: [ + "id", + "creationTime", + "creatorId", + "lastModificationTime", + "lastModifierId", + "isDeleted", + "deleterId", + "deletionTime", + "parentCompanyId", + ], + formPositions: [ + "name", + "taxpayerId", + "legalStatusCode", + "customerNumber", + "areaCode", + "localNumber", + "ituCountryCode", + "primaryFlag", + "telephoneTypeCode", + "addressLine", + "city", + "terriority", + "postalCode", + "country", + "fullAddress", + "addressPrimaryFlag", + "addressTypeCode", + "emailAddress", + "emailPrimaryFlag", + "emailTypeCode", + "productName", + "vatRate", + "productCode", + "isActive", + ], schema: $showMerchants, }, }, - refund_points: { createFormSchema: { formPositions: ["Company", "CustomerNumber", "ProductGroups", "Address"], @@ -97,7 +180,6 @@ const dataConfig: Record = { }, filterBy: "Company", }, - customs: { createFormSchema: { formPositions: ["Company", "CustomerNumber", "ProductGroups", "Address"], @@ -113,7 +195,6 @@ const dataConfig: Record = { }, filterBy: "Company", }, - tax_free: { filterBy: "Company", createFormSchema: { @@ -129,7 +210,6 @@ const dataConfig: Record = { excludeList: ["Company", "CustomerNumber", "ProductGroups", "Address"], }, }, - tax_offices: { filterBy: "Company", createFormSchema: { @@ -146,7 +226,6 @@ const dataConfig: Record = { }, }, }; - function convertEnumField( value: string | number, enumArray: string[], @@ -156,7 +235,6 @@ function convertEnumField( } return enumArray.indexOf(value); } - export default function Page({ params, }: { @@ -165,7 +243,6 @@ export default function Page({ const [roles, setRoles] = useState(); const [isLoading, setIsLoading] = useState(true); const fetchLink = getBaseLink(`/api/company/${params.data}`); - function getRoles() { function onData(data: any) { let returnData = data; @@ -225,12 +302,10 @@ export default function Page({ ); }, }; - useEffect(() => { setIsLoading(true); getRoles(); }, []); - function parseFormValues(schema: formModifier, data: any) { const newSchema = createZodObject( schema.schema, @@ -244,13 +319,11 @@ export default function Page({ Object.entries(schema.convertors).forEach(([key, value]) => { returnObject[key] = convertEnumField(returnObject[key], value); }); - return returnObject; }); const parsed = transformedSchema.parse(data); return parsed; } - const onEdit = (data: any, row: any, editFormSchema: any) => { const parsedData = parseFormValues(editFormSchema, data); controlledFetch( @@ -266,7 +339,6 @@ export default function Page({ "Updated Successfully", ); }; - const onDelete = (e: any, row: any) => { controlledFetch( fetchLink, @@ -278,7 +350,6 @@ export default function Page({ "Deleted Successfully", ); }; - function convertZod(schema: formModifier) { const newSchema = createZodObject( schema.schema, @@ -289,7 +360,6 @@ export default function Page({ } const editFormSchema = dataConfig[params.data].editFormSchema; const editFormSchemaZod = convertZod(editFormSchema); - const columnsData: columnsType = { type: "Auto", data: { @@ -307,7 +377,6 @@ export default function Page({ onDelete, }, }; - return ( {children} diff --git a/apps/web/src/app/api/company/[data]/route.ts b/apps/web/src/app/api/company/[data]/route.ts index 01ed97d40..b4e2f591d 100644 --- a/apps/web/src/app/api/company/[data]/route.ts +++ b/apps/web/src/app/api/company/[data]/route.ts @@ -1,6 +1,5 @@ import type { Volo_Abp_Http_RemoteServiceErrorResponse } from "@ayasofyazilim/saas/AccountService"; import { ApiError } from "@ayasofyazilim/saas/IdentityService"; -import type { GetApiMerchantServiceMerchantsDetailResponse } from "@ayasofyazilim/saas/MerchantService"; import type { NextRequest } from "next/server"; import { getIdentityServiceClient, @@ -20,86 +19,188 @@ function isApiError(error: unknown): error is ApiError { const clients: Clients = { merchants: async () => { const client = await getMerchantServiceClient(); - const merchant = client.merchant; + const merchant = client.organization; return { get: async () => { - const getDetails: GetApiMerchantServiceMerchantsDetailResponse = - await merchant.getApiMerchantServiceMerchantsDetail({ - maxResultCount: 1000, - }); - return ( - getDetails.items?.map((item) => { - const organization = - item.entityInformations?.[0]?.organizations?.[0]; - return { - Company: organization?.name || "", - CustomerNumber: organization?.customerNumber || "", - ProductGroups: - organization?.productGroups?.map((pg) => pg.name) || [], - Address: - organization?.contactInformation?.addresses?.[0]?.fullAddress || - "", - }; - }) || [] - ); + const getDetails = + await merchant.getApiMerchantServiceOrganizationsDetail(); + + return getDetails.items?.map((organization) => { + const contactInfo = organization.contactInformation || {}; + const telephone = contactInfo.telephones?.[0] || {}; + const address = contactInfo.addresses?.[0] || {}; + const email = contactInfo.emails?.[0] || {}; + const productGroup = organization.productGroups?.[0] || {}; + + return { + id: organization.id || "", + name: organization.name || "", + taxpayerId: organization.taxpayerId || "", + legalStatusCode: organization.legalStatusCode || "", + customerNumber: organization.customerNumber || "", + areaCode: telephone.areaCode || "", + localNumber: telephone.localNumber || "", + ituCountryCode: telephone.ituCountryCode || "", + primaryFlag: telephone.primaryFlag || false, + telephoneTypeCode: telephone.typeCode || 0, + addressLine: address.addressLine || "", + city: address.city || "", + terriority: address.terriority || "", + postalCode: address.postalCode || "", + country: address.country || "", + fullAddress: address.fullAddress || "", + addressPrimaryFlag: address.primaryFlag || false, + addressTypeCode: address.typeCode || 0, + emailAddress: email.emailAddress || "", + emailPrimaryFlag: email.primaryFlag || false, + emailTypeCode: email.typeCode || 0, + productName: productGroup.name || "", + vatRate: productGroup.vatRate || 0, + productCode: productGroup.productCode || "", + isActive: productGroup.isActive || false, + }; + }); }, - post: (formdata: any) => { - return formdata; - // return merchant.postApiMerchantServiceMerchantsCreateMerchantWithComponents( - // { - // requestBody: { - // entityInformationTypes: [ - // { - // organizations: [ - // { - // name: formdata.Company, - // taxpayerId: "string", - // legalStatusCode: "string", - // customerNumber: formdata.CustomerNumber, - // contactInformation: { - // startDate: "2024-06-27T10:53:06.853Z", - // endDate: "2024-06-27T10:53:06.853Z", - // telephone: [ - // { - // areaCode: "string", - // localNumber: "string", - // ituCountryCode: "string", - // }, - // ], - // address: [ - // { - // typeCode: 0, - // addressLine: "string", - // city: "string", - // terriority: "string", - // postalCode: "string", - // country: "string", - // fullAddress: formdata.Address, - // }, - // ], - // email: [ - // { - // emailAddress: "string", - // }, - // ], - // }, - // productGroups: [ - // { - // name: formdata.ProductGroups, - // vatRate: 0, - // productCode: "string", - // isActive: true, - // }, - // ], - // }, - // ], - // }, - // ], - // }, - // }, - // ); + post: async (formdata: any) => + merchant.postApiMerchantServiceOrganizations({ + requestBody: { + name: formdata.name, + taxpayerId: formdata.taxpayerId, + legalStatusCode: formdata.legalStatusCode, + customerNumber: formdata.customerNumber, + contactInformation: { + telephones: [ + { + areaCode: formdata.areaCode, + localNumber: formdata.localNumber, + ituCountryCode: formdata.ituCountryCode, + primaryFlag: formdata.primaryFlag, + typeCode: formdata.telephoneTypeCode, + }, + ], + addresses: [ + { + addressLine: formdata.addressLine, + city: formdata.city, + terriority: formdata.terriority, + postalCode: formdata.postalCode, + country: formdata.country, + fullAddress: formdata.fullAddress, + primaryFlag: formdata.addressPrimaryFlag, + typeCode: formdata.addressTypeCode, + }, + ], + emails: [ + { + emailAddress: formdata.emailAddress, + primaryFlag: formdata.emailPrimaryFlag, + typeCode: formdata.emailTypeCode, + }, + ], + }, + productGroups: [ + { + name: formdata.productName, + vatRate: formdata.vatRate, + productCode: formdata.productCode, + isActive: formdata.isActive, + }, + ], + }, + entityInformationTypeId: "e5f7f9e0-ceee-71f6-7b93-3a136c155b82", + }), + put: async (requestBody: { + id: string; + requestBody: { + name: string; + taxpayerId: string; + legalStatusCode: string; + customerNumber: string; + areaCode: string; + localNumber: string; + ituCountryCode: string; + primaryFlag: boolean; + telephoneTypeCode: number; + addressLine: string; + city: string; + terriority: string; + postalCode: string; + country: string; + fullAddress: string; + addressPrimaryFlag: boolean; + addressTypeCode: number; + emailAddress: string; + emailPrimaryFlag: boolean; + emailTypeCode: number; + productName: string; + vatRate: number; + productCode: string; + isActive: boolean; + }; + }) => { + const currentDetails = + await merchant.getApiMerchantServiceOrganizationsDetailById({ + id: requestBody.id, + }); + + const updatedDetails = { + ...currentDetails, + name: requestBody.requestBody.name, + taxpayerId: requestBody.requestBody.taxpayerId, + legalStatusCode: requestBody.requestBody.legalStatusCode, + customerNumber: requestBody.requestBody.customerNumber, + contactInformation: { + ...currentDetails.contactInformation, + telephones: [ + { + ...currentDetails.contactInformation?.telephones?.[0], + areaCode: requestBody.requestBody.areaCode, + localNumber: requestBody.requestBody.localNumber, + ituCountryCode: requestBody.requestBody.ituCountryCode, + primaryFlag: requestBody.requestBody.primaryFlag, + typeCode: requestBody.requestBody.telephoneTypeCode, + }, + ], + addresses: [ + { + ...currentDetails.contactInformation?.addresses?.[0], + addressLine: requestBody.requestBody.addressLine, + city: requestBody.requestBody.city, + terriority: requestBody.requestBody.terriority, + postalCode: requestBody.requestBody.postalCode, + country: requestBody.requestBody.country, + fullAddress: requestBody.requestBody.fullAddress, + primaryFlag: requestBody.requestBody.addressPrimaryFlag, + typeCode: requestBody.requestBody.addressTypeCode, + }, + ], + emails: [ + { + ...currentDetails.contactInformation?.emails?.[0], + emailAddress: requestBody.requestBody.emailAddress, + primaryFlag: requestBody.requestBody.emailPrimaryFlag, + typeCode: requestBody.requestBody.emailTypeCode, + }, + ], + }, + productGroups: [ + { + ...currentDetails.productGroups?.[0], + name: requestBody.requestBody.productName, + vatRate: requestBody.requestBody.vatRate, + productCode: requestBody.requestBody.productCode, + isActive: requestBody.requestBody.isActive, + }, + ], + }; + + return merchant.putApiMerchantServiceOrganizations({ + requestBody: updatedDetails as any, + }); }, + delete: async (id: string) => + merchant.deleteApiMerchantServiceOrganizations({ id }), }; }, @@ -335,7 +436,7 @@ const clients: Clients = { export async function GET( request: NextRequest, - { params }: { params: { data: string } }, + { params }: { params: { data: string } } ) { if (!clients[params.data]) { // return status 404 @@ -358,7 +459,7 @@ export async function GET( export async function POST( request: NextRequest, - { params }: { params: { data: string } }, + { params }: { params: { data: string } } ) { if (!clients[params.data]) { return errorResponse("Invalid data type"); @@ -373,7 +474,7 @@ export async function POST( const body = error.body as Volo_Abp_Http_RemoteServiceErrorResponse; return errorResponse( body.error?.message || "Something went wrong", - error.status, + error.status ); } return errorResponse("Something went wrong"); @@ -382,7 +483,7 @@ export async function POST( export async function DELETE( request: NextRequest, - { params }: { params: { data: string } }, + { params }: { params: { data: string } } ) { if (!clients[params.data]) { return errorResponse("Invalid data type"); @@ -397,7 +498,7 @@ export async function DELETE( export async function PUT( request: NextRequest, - { params }: { params: { data: string } }, + { params }: { params: { data: string } } ) { if (!clients[params.data]) { return errorResponse("Invalid data type"); @@ -415,7 +516,7 @@ export async function PUT( const body = error.body as Volo_Abp_Http_RemoteServiceErrorResponse; return errorResponse( body.error?.message || "Something went wrong", - error.status, + error.status ); } return errorResponse("Something went wrong");