Skip to content

Commit

Permalink
Adjust enrollment typing
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Dec 10, 2024
1 parent a96dda7 commit d66c33c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ export default async function createEnrollment(

if (product.type !== ProductTypes.PlanProduct) throw new ProductWrongTypeError({ type: product.type });

const enrollment = await modules.enrollments.create(
{
billingAddress,
configuration,
contact,
countryCode: countryContext,
currencyCode: currencyContext,
delivery,
meta,
payment,
productId,
quantity,
userId,
},
context,
);
const enrollment = await modules.enrollments.create({
billingAddress,
configuration,
contact,
countryCode: countryContext,
currencyCode: currencyContext,
delivery,
meta,
payment,
productId,
quantity,
userId,
});

return await services.enrollments.initializeEnrollment(
enrollment,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { log } from '@unchainedshop/logger';
import { Context } from '../../../context.js';
import { ProductNotFoundError, InvalidIdError } from '../../../errors.js';
import { resolveBestCurrency } from '@unchainedshop/utils';

export default async function requestQuotation(
root: never,
params: { productId: string; configuration: Array<{ key: string; value: string }> },
context: Context,
) {
const { countryContext, modules, services, userId } = context;
const { countryContext, currencyContext, modules, services, userId } = context;
const { productId, configuration } = params;

log(`mutation requestQuotation ${productId} ${configuration ? JSON.stringify(configuration) : ''}`, {
Expand All @@ -20,15 +19,11 @@ export default async function requestQuotation(
if (!(await modules.products.productExists({ productId })))
throw new ProductNotFoundError({ productId });

const countryObject = await modules.countries.findCountry({ isoCode: countryContext });
const currencies = await modules.currencies.findCurrencies({ includeInactive: false });
const currency = resolveBestCurrency(countryObject.defaultCurrencyCode, currencies);

const newQuotation = await modules.quotations.create({
userId,
productId,
countryCode: countryContext,
currency,
currency: currencyContext,
configuration,
});

Expand Down
19 changes: 6 additions & 13 deletions packages/core-enrollments/src/module/configureEnrollmentsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from '@unchainedshop/mongodb';
import { EnrollmentsCollection } from '../db/EnrollmentsCollection.js';
import { enrollmentsSettings, EnrollmentsSettingsOptions } from '../enrollments-settings.js';
import { resolveBestCurrency } from '@unchainedshop/utils';

export type EnrollmentQuery = {
status?: Array<EnrollmentStatus>;
Expand Down Expand Up @@ -215,24 +214,18 @@ export const configureEnrollmentsModule = async ({
return enrollment;
},

create: async (
{ countryCode, currencyCode, ...enrollmentData }: Omit<Enrollment, 'status' | 'periods' | 'log'>,
unchainedAPI,
): Promise<Enrollment> => {
const { modules } = unchainedAPI;

const countryObject = await modules.countries.findCountry({ isoCode: countryCode });
const currencies = await modules.currencies.findCurrencies({ includeInactive: false });
const currency =
currencyCode || resolveBestCurrency(countryObject.defaultCurrencyCode, currencies);

create: async ({
countryCode,
currencyCode,
...enrollmentData
}: Omit<Enrollment, 'status' | 'periods' | 'log'>): Promise<Enrollment> => {
const { insertedId: enrollmentId } = await Enrollments.insertOne({
_id: generateDbObjectId(),
created: new Date(),
...enrollmentData,
status: EnrollmentStatus.INITIAL,
periods: [],
currencyCode: currency,
currencyCode,
countryCode,
configuration: enrollmentData.configuration || [],
log: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/createEnrollmentFromCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const createEnrollmentFromCheckoutService = async (
unchainedAPI,
);

const enrollment = await modules.enrollments.create(enrollmentData, unchainedAPI);
const enrollment = await modules.enrollments.create(enrollmentData);
return await initializeEnrollmentService(
enrollment,
{
Expand Down

0 comments on commit d66c33c

Please sign in to comment.