Skip to content

Commit

Permalink
✨ (shopify/index.ts, validators.ts): Introduce MintLoyaltyCardWithCus…
Browse files Browse the repository at this point in the history
…tomerId functionality to support customer ID based loyalty card minting, enhancing flexibility and user identification methods.

♻️ (shopify/index.ts): Refactor Shopify handler to use new validation methods for improved code maintainability and to accommodate new minting process based on customer ID.
📝 (shopify/index.ts): Update comments and method names to reflect new functionality and deprecate old password-based minting method in favor of customer ID approach.
  • Loading branch information
sebpalluel committed Apr 19, 2024
1 parent 90d7fc7 commit 5adf4c0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 35 deletions.
94 changes: 61 additions & 33 deletions libs/integrations/external-api-handlers/src/lib/shopify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,32 @@ import { getErrorMessage } from '@utils';
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { BaseWebhookAndApiHandler } from '../baseWebhookAndApiHandler';
import { HasLoyaltyCardParams, MintLoyaltyCardParams } from './validators';
import {
HasLoyaltyCardParams,
MintLoyaltyCardWithCustomerIdParams,
MintLoyaltyCardWithPasswordParams,
} from './validators';

export enum RequestType {
MintLoyaltyCard = 'MintLoyaltyCard',
MintLoyaltyCardWithPassword = 'MintLoyaltyCardWithPassword',
MintLoyaltyCardWithCustomerId = 'MintLoyaltyCardWithCustomerId',
HasLoyaltyCard = 'HasLoyaltyCard',
}

const requestTypeValidators = {
[RequestType.MintLoyaltyCard]: MintLoyaltyCardParams,
[RequestType.MintLoyaltyCardWithPassword]: MintLoyaltyCardWithPasswordParams,
[RequestType.MintLoyaltyCardWithCustomerId]:
MintLoyaltyCardWithCustomerIdParams,
[RequestType.HasLoyaltyCard]: HasLoyaltyCardParams,
};

type RequestTypeToValidator = {
[RequestType.MintLoyaltyCard]: z.infer<typeof MintLoyaltyCardParams>;
[RequestType.MintLoyaltyCardWithPassword]: z.infer<
typeof MintLoyaltyCardWithPasswordParams
>;
[RequestType.MintLoyaltyCardWithCustomerId]: z.infer<
typeof MintLoyaltyCardWithCustomerIdParams
>;
[RequestType.HasLoyaltyCard]: z.infer<typeof HasLoyaltyCardParams>;
};

Expand Down Expand Up @@ -91,6 +103,25 @@ export class ShopifyWebhookAndApiHandler extends BaseWebhookAndApiHandler {
};
}

private async extractAndValidateShopifyParams<T extends RequestType>(
req: NextRequest,
requestType: T,
): Promise<RequestTypeToValidator[T]> {
const { resultParams } = await this.extractAndVerifyShopifyRequest(
req,
).catch((error) => {
throw new NotAuthorizedError('Not Authorized: ' + getErrorMessage(error));
});

return this.serializeAndValidateParams(requestType, resultParams).catch(
(error: Error) => {
throw new BadRequestError(
'Invalid query parameters: ' + getErrorMessage(error),
);
},
);
}

private populateQueryHash(searchParams: URLSearchParams): string {
// Create a new instance of URLSearchParams to ensure we're not modifying the original
const filteredParams = new URLSearchParams(searchParams);
Expand Down Expand Up @@ -130,33 +161,40 @@ export class ShopifyWebhookAndApiHandler extends BaseWebhookAndApiHandler {
});
}

// deprecated (replaced by mintLoyaltyCardWithCustomerId)
mintLoyaltyCardWithPassword = handleApiRequest<MintLoyaltyCardOptions>(
mintLoyaltyCardWithCustomerId = handleApiRequest<MintLoyaltyCardOptions>(
async (options) => {
// Destructure options and provide default value for loyaltyCardSdk
const { req, contractAddress } = options;

const loyaltyCardSdk =
options.loyaltyCardSdk || new LoyaltyCardNftWrapper();

// Extract and verify Shopify request
const { resultParams } = await this.extractAndVerifyShopifyRequest(
req,
).catch((error) => {
throw new NotAuthorizedError(
'Not Authorized: ' + getErrorMessage(error),
const { ownerAddress, customerId } =
await this.extractAndValidateShopifyParams(
req,
RequestType.MintLoyaltyCardWithCustomerId,
);
});

// Serialize and validate parameters
const validatedParams = await this.serializeAndValidateParams(
RequestType.MintLoyaltyCard,
resultParams,
).catch((error: Error) => {
throw new BadRequestError(
'Invalid query parameters: ' + getErrorMessage(error),
);
return new NextResponse(JSON.stringify({}), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
},
);

// deprecated (replaced by mintLoyaltyCardWithCustomerId)
mintLoyaltyCardWithPassword = handleApiRequest<MintLoyaltyCardOptions>(
async (options) => {
// Destructure options and provide default value for loyaltyCardSdk
const { req, contractAddress } = options;

const loyaltyCardSdk =
options.loyaltyCardSdk || new LoyaltyCardNftWrapper();

const validatedParams = await this.extractAndValidateShopifyParams(
req,
RequestType.MintLoyaltyCardWithPassword,
);

// Prepare data for minting
const mintData: MintLoyaltyCardWithPasswordProps = {
Expand Down Expand Up @@ -196,20 +234,10 @@ export class ShopifyWebhookAndApiHandler extends BaseWebhookAndApiHandler {
async hasLoyaltyCard(options: ApiHandlerOptions, contractAddress: string) {
const { req } = options;

const { resultParams } = await this.extractAndVerifyShopifyRequest(
const { ownerAddress } = await this.extractAndValidateShopifyParams(
req,
).catch((error) => {
throw new NotAuthorizedError('Not Authorized: ' + getErrorMessage(error));
});

const { ownerAddress } = await this.serializeAndValidateParams(
RequestType.HasLoyaltyCard,
resultParams,
).catch((error: Error) => {
throw new BadRequestError(
'Invalid query parameters: ' + getErrorMessage(error),
);
});
);

const nftExists = await this.checkNftExistence(
ownerAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { z } from 'zod';

export const MintLoyaltyCardParams = z.object({
export const MintLoyaltyCardWithPasswordParams = z.object({
password: z.string(),
ownerAddress: z.string(),
email: z.string().email(),
});

export const MintLoyaltyCardWithCustomerIdParams = z.object({
ownerAddress: z.string(),
customerId: z.string(),
});

export const HasLoyaltyCardParams = z.object({
Expand Down

0 comments on commit 5adf4c0

Please sign in to comment.