Skip to content

Commit

Permalink
refactor adapter functions from useApiPostSubsidiary into a standalon…
Browse files Browse the repository at this point in the history
…e adapter
  • Loading branch information
Šimon Macek committed Dec 11, 2024
1 parent 9785501 commit 34ab991
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 43 deletions.
41 changes: 41 additions & 0 deletions src/adapters/subsidiaryAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// types
import type { FormCompanyAddressFields } from '../components/types/Form';
import type {
SubsidiaryPostApiPayload,
SubsidiaryPostApiResponse,
} from '../components/types/ApiSubsidiary';

/**
* Adapter for converting between API and form subsidiary data formats
*/
export const subsidiaryAdapter = {
/**
* Convert form data to API payload format
*/
toApiPayload(formData: FormCompanyAddressFields): SubsidiaryPostApiPayload {
return {
address: {
street: formData.street,
street_number: formData.houseNumber,
recipient: formData.department,
city: formData.city,
psc: formData.zip,
},
city_id: formData.cityChallenge,
};
},

/**
* Convert API response to form data format
*/
toFormData(apiData: SubsidiaryPostApiResponse): FormCompanyAddressFields {
return {
street: apiData.address.street,
houseNumber: apiData.address.street_number,
city: apiData.address.city,
zip: String(apiData.address.psc),
cityChallenge: apiData.city_id,
department: apiData.address.recipient,
};
},
};
23 changes: 23 additions & 0 deletions src/components/types/ApiSubsidiary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* API types for subsidiary-related operations
*/

export interface SubsidiaryPostApiAddress {
street: string;
street_number: string;
recipient: string;
city: string;
psc: string | number;
}

export interface SubsidiaryPostApiPayload {
address: SubsidiaryPostApiAddress;
city_id: number | null;
}

export interface SubsidiaryPostApiResponse {
id: number;
city_id: number | null;
active: boolean;
address: SubsidiaryPostApiAddress;
}
53 changes: 10 additions & 43 deletions src/composables/useApiPostSubsidiary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// libraries
import { ref, Ref } from 'vue';

// adapters
import { subsidiaryAdapter } from '../adapters/subsidiaryAdapter';

// composables
import { useApi } from './useApi';

Expand All @@ -13,30 +16,11 @@ import { useLoginStore } from '../stores/login';
// types
import type { FormCompanyAddressFields } from '../components/types/Form';
import type { Logger } from '../components/types/Logger';
import type { SubsidiaryPostApiResponse } from '../components/types/ApiSubsidiary';

// utils
import { requestDefaultHeader, requestTokenHeader } from '../utils';

interface PostSubsidiaryPayload {
address: PostSubsidiaryAddressFields;
city_id: number | null;
}

interface PostSubsidiaryAddressFields {
street: string;
street_number: string;
recipient: string;
city: string;
psc: string | number;
}

interface PostSubsidiaryResponse {
id: number;
city_id: number | null;
active: boolean;
address: PostSubsidiaryAddressFields;
}

interface UseApiPostSubsidiaryReturn {
isLoading: Ref<boolean>;
createSubsidiary: (
Expand All @@ -62,27 +46,18 @@ export const useApiPostSubsidiary = (
* Create subsidiary
* Creates a new subsidiary under specified organization
* @param {number} organizationId - Organization Id to create subsidiary under
* @param {PostSubsidiaryPayload} subsidiaryData - Subsidiary data to create
* @returns {Promise<PostSubsidiaryResponse | null>} - Promise
* @param {FormCompanyAddressFields} subsidiaryData - Subsidiary data to create
* @returns {Promise<FormCompanyAddressFields | null>} - Promise
*/
const createSubsidiary = async (
organizationId: number,
subsidiaryData: FormCompanyAddressFields,
): Promise<FormCompanyAddressFields | null> => {
// parse subsidiary data and create a paylaod for API
// convert form data to API payload format
logger?.debug(
`Creating subsidiary payload from data <${JSON.stringify(subsidiaryData, null, 2)}>.`,
);
const subsidiaryPayload: PostSubsidiaryPayload = {
address: {
street: subsidiaryData.street,
street_number: subsidiaryData.houseNumber,
recipient: subsidiaryData.department,
city: subsidiaryData.city,
psc: subsidiaryData.zip,
},
city_id: subsidiaryData.cityChallenge,
};
const subsidiaryPayload = subsidiaryAdapter.toApiPayload(subsidiaryData);
logger?.debug(
`Created subsidiary payload <${JSON.stringify(subsidiaryPayload, null, 2)}>.`,
);
Expand All @@ -94,7 +69,7 @@ export const useApiPostSubsidiary = (

try {
// post subsidiary
const { data } = await apiFetch<PostSubsidiaryResponse>({
const { data } = await apiFetch<SubsidiaryPostApiResponse>({
endpoint: `${rideToWorkByBikeConfig.urlApiOrganizations}${organizationId}/${rideToWorkByBikeConfig.urlApiSubsidiaries}`,
method: 'post',
translationKey: 'createSubsidiary',
Expand All @@ -109,15 +84,7 @@ export const useApiPostSubsidiary = (
logger?.debug(
`Parsing response subsidiary data <${JSON.stringify(data, null, 2)}>.`,
);
// parse subsidiary data to return
const subsidiaryDataParsed: FormCompanyAddressFields = {
street: data.address.street,
houseNumber: data.address.street_number,
city: data.address.city,
zip: data.address.psc ? String(data.address.psc) : '',
cityChallenge: data.city_id,
department: data.address.recipient,
};
const subsidiaryDataParsed = subsidiaryAdapter.toFormData(data);
logger?.debug(
`Parsed subsidiary data <${JSON.stringify(subsidiaryDataParsed, null, 2)}>.`,
);
Expand Down

0 comments on commit 34ab991

Please sign in to comment.