Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add field definition for custom type #34

Merged
merged 2 commits into from
May 24, 2024
Merged
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
42 changes: 34 additions & 8 deletions shared/src/commercetools/customTypeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ export async function createCustomOrderType(): Promise<string | undefined> {
try {
const typeKey = await orderCustomTypeKey();
if (typeKey) {
const fieldDefinitions = await requiredFieldDefinitions();
const customType: TypeDraft = {
key: typeKey,
name: {
en: CUSTOM_TYPE_NAME,
},
resourceTypeIds: ['order'],
fieldDefinitions: customTypeDefinition.orderCustomTypeFieldDefinitions as FieldDefinition[],
fieldDefinitions,
};
const result = await createApiRoot().types().post({ body: customType }).execute();

Expand All @@ -97,7 +98,7 @@ export async function updateCustomOrderType(id: string): Promise<string | undefi
if (typeKey) {
const customType = await getCustomTypeById(id);
if (customType) {
const missingFields = listOfMissingFields(customType.fieldDefinitions);
const missingFields = await listOfMissingFields(customType.fieldDefinitions);
if (missingFields.length > 0) {
const updateActions = missingFields.map((f) => addFieldDefinitionAction(f));
const typeUpdate = updateTypeAction(customType, updateActions);
Expand Down Expand Up @@ -130,12 +131,37 @@ export async function orderCustomTypeKey(): Promise<string | undefined> {
return key;
}

function listOfMissingFields(existingFields: FieldDefinition[]): FieldDefinition[] {
const requiredFieldNames = customTypeDefinition.orderCustomTypeFieldDefinitions.map((f) => f.name);
async function collectChannelReferenceFieldName(): Promise<string | undefined> {
let name: string | undefined;
const configuration = await getConfiguration();
if (configuration) {
name = configuration.collectChannelReferenceFieldName;
}
return name;
}

async function requiredFieldDefinitions(): Promise<FieldDefinition[]> {
const requiredFieldDefinitions = customTypeDefinition.orderCustomTypeFieldDefinitions as FieldDefinition[];
const fieldName = await collectChannelReferenceFieldName();
if (fieldName) {
requiredFieldDefinitions.push({
name: fieldName,
label: {
en: 'FFT Click & Collect Facility',
},
required: false,
type: {
name: 'String',
},
});
}
return requiredFieldDefinitions;
}

async function listOfMissingFields(existingFields: FieldDefinition[]): Promise<FieldDefinition[]> {
const requiredFields = await requiredFieldDefinitions();
const requiredFieldNames = requiredFields.map((f) => f.name);
const existingFieldNames = existingFields.map((f) => f.name);
const missingFieldNames = requiredFieldNames.filter((f) => !existingFieldNames.includes(f));
const missingFields = customTypeDefinition.orderCustomTypeFieldDefinitions.filter((f) =>
missingFieldNames.includes(f.name)
);
return missingFields as FieldDefinition[];
return requiredFields.filter((f) => missingFieldNames.includes(f.name));
}
Loading