Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
const handleResetAnnualFeeServices = () => {
        if (selectedAnnualFee && annualFeeServices?.length && body?.services?.length) {
            const serviceIds = annualFeeServices.reduce((acc, cur) => {
                return {
                    ...acc,
                    [cur.id]: cur?.quantity || 1,
                };
            }, {});

            const removedDuplicateServices = Object.values(
                body?.services?.reduce((acc, cur) => {
                    let key = `${cur.id}`;
                    if (!acc[key]) acc[key] = { ...cur, quantity: cur?.quantity || 1 };
                    else acc[key].quantity += 1;
                    return acc;
                }, {}),
            );

            const result = removedDuplicateServices
                ?.map((service) => {
                    if (serviceIds[service.id]) {
                        const serviceTemp = {
                            ...service,
                            quantity: service.quantity - serviceIds[service.id],
                        };
                        console.log({ serviceTemp });
                        if (serviceTemp.quantity === 0) {
                            return null;
                        } else {
                            return serviceTemp;
                        }
                    } else {
                        return service;
                    }
                })
                .filter((service) => service);

            setBody({
                ...body,
                services: result,
            });
        }
    };