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

Product variation assignment fixs #624

Open
wants to merge 4 commits into
base: v2.x
Choose a base branch
from
Open
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
262 changes: 253 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/api/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,8 @@ export const ImpersonatingAdminUserError = createError(
export const FileNotFoundError = createError('FileNotFoundError', 'File not found');

export const FileUploadExpiredError = createError('FileUploadExpired', 'File upload has expired');

export const ConfigurationVectorInvalid = createError(
'ConfigurationVectorInvalid',
'Provider Configuration vector is invalid/incomplete please provide all available configuration vectors',
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@ import { ProductTypes } from '@unchainedshop/core-products';

import { Context, Root } from '@unchainedshop/types/api.js';
import { ProductConfiguration } from '@unchainedshop/types/products.js';
import { ProductNotFoundError, InvalidIdError, ProductWrongTypeError } from '../../../errors.js';
import {
ProductNotFoundError,
InvalidIdError,
ProductWrongTypeError,
ConfigurationVectorInvalid,
} from '../../../errors.js';
const extractVariationMatrix = (variations = []) => {
const cartesianProduct = (arrays) => {
return arrays.reduce(
(acc, array) => acc.flatMap((item) => array.map((value) => [...item, value])),
[[]],
);
};
const keys = variations.map((item) => item.key);
const options = variations.map((item) => item.options);
const combinations = cartesianProduct(options);
return combinations.map((combination) =>
combination.reduce((acc, value, index) => {
acc[keys[index]] = value;
return acc;
}, {}),
);
};

const combinationExists = (matrix, combination) => {
return matrix.some((variation) => {
return Object.entries(combination).every(([key, value]) => variation[key] === value);
});
};

export default async function addProductAssignment(
root: Root,
Expand Down Expand Up @@ -35,6 +63,18 @@ export default async function addProductAssignment(
received: proxyProduct.type,
required: ProductTypes.ConfigurableProduct,
});
const variations = await modules.products.variations.findProductVariations({
productId: proxyId,
});
const variationMatrix = extractVariationMatrix(variations);
const normalizedVectors = vectors?.reduce((prev, { key, value }) => {
return {
...prev,
[key]: value,
};
}, {});
if (!combinationExists(variationMatrix, normalizedVectors))
throw new ConfigurationVectorInvalid({ proxyId, vectors });

await modules.products.assignments.addProxyAssignment(productId, {
proxyId,
Expand Down
Loading