Skip to content

Commit

Permalink
feat(accept-blue): fix pagination for blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug committed Jan 14, 2025
1 parent 25dfdbd commit d86f434
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 78 deletions.
4 changes: 4 additions & 0 deletions packages/vendure-plugin-accept-blue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.2.0 (2025-01-14)

- Update a customer's shipping and billing details in Accept Blue on subscription creation

# 2.1.0 (2025-01-10)

- Allow updating created subscriptions via the Admin API
Expand Down
2 changes: 1 addition & 1 deletion packages/vendure-plugin-accept-blue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-plugin-accept-blue",
"version": "2.1.0",
"version": "2.2.0",
"description": "Vendure plugin for creating subscriptions with the Accept Blue platform",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
46 changes: 38 additions & 8 deletions packages/vendure-plugin-accept-blue/src/api/accept-blue-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import { loggerCtx } from '../constants';
import {
AcceptBlueChargeTransaction,
AcceptBlueCustomer,
AcceptBlueCustomerInput,
AcceptBluePaymentMethod,
AcceptBlueRecurringSchedule,
AcceptBlueRecurringScheduleCreateInput,
AcceptBlueRecurringScheduleTransaction,
CheckPaymentMethodInput,
NoncePaymentMethodInput,
AcceptBlueRecurringScheduleUpdateInput,
AcceptBlueTransaction,
AcceptBlueWebhookInput,
AcceptBlueWebhook,
AcceptBlueWebhookInput,
CheckPaymentMethodInput,
CustomFields,
AcceptBlueRecurringScheduleUpdateInput,
NoncePaymentMethodInput,
} from '../types';
import { isSameCard, isSameCheck } from '../util';

Expand Down Expand Up @@ -53,13 +54,27 @@ export class AcceptBlueClient {
return await this.request('get', `transactions/${id}`);
}

async getOrCreateCustomer(emailAddress: string): Promise<AcceptBlueCustomer> {
/**
* Find a customer based on given emailAddress and updates the details.
* If no customer found, creates a new customer.
*/
async upsertCustomer(
emailAddress: string,
input: AcceptBlueCustomerInput
): Promise<AcceptBlueCustomer> {
const existing = await this.getCustomer(emailAddress);
if (existing) {
await this.updateCustomer(existing.id, input).catch((e) => {
// Catch and log instead of throw, because an existing customer was already found to return
Logger.error(
`Failed to update customer ${existing.id}: ${e}`,
loggerCtx
);
});
return existing;
} else {
Logger.info(`Creating new customer ${emailAddress}`, loggerCtx);
return await this.createCustomer(emailAddress);
return await this.createCustomer(emailAddress, input);
}
}

Expand All @@ -85,8 +100,12 @@ export class AcceptBlueClient {
return undefined;
}

async createCustomer(emailAddress: string): Promise<AcceptBlueCustomer> {
const customer: Partial<AcceptBlueCustomer> = {
async createCustomer(
emailAddress: string,
input: AcceptBlueCustomerInput
): Promise<AcceptBlueCustomer> {
const customer: AcceptBlueCustomerInput = {
...input,
identifier: emailAddress,
customer_number: emailAddress,
email: emailAddress,
Expand All @@ -99,6 +118,17 @@ export class AcceptBlueClient {
return result;
}

async updateCustomer(
id: number,
input: AcceptBlueCustomerInput
): Promise<AcceptBlueCustomer> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await this.request('patch', `customers/${id}`, input);
Logger.info(`Updated customer '${id}'`, loggerCtx);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result;
}

async getOrCreatePaymentMethod(
acceptBlueCustomerId: number,
input: NoncePaymentMethodInput | CheckPaymentMethodInput
Expand Down
55 changes: 50 additions & 5 deletions packages/vendure-plugin-accept-blue/src/api/accept-blue-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import {
TransactionalConnection,
UserInputError,
} from '@vendure/core';
import { asError } from 'catch-unknown';
import crypto from 'node:crypto';
import { filter } from 'rxjs';
import { In } from 'typeorm';
import { asError } from 'catch-unknown';
import * as util from 'util';
import { SubscriptionHelper } from '../';
import { AcceptBluePluginOptions } from '../accept-blue-plugin';
import { PLUGIN_INIT_OPTIONS, loggerCtx } from '../constants';
import { AcceptBlueSubscriptionEvent } from '../events/accept-blue-subscription-event';
import { AcceptBlueTransactionEvent } from '../events/accept-blue-transaction-event';
import {
AcceptBlueChargeTransaction,
AcceptBlueCustomerInput,
AcceptBlueEvent,
AcceptBluePaymentMethod,
AcceptBlueRecurringSchedule,
Expand All @@ -54,8 +57,6 @@ import {
AcceptBlueTransaction,
UpdateAcceptBlueSubscriptionInput,
} from './generated/graphql';
import { AcceptBlueTransactionEvent } from '../events/accept-blue-transaction-event';
import { AcceptBlueSubscriptionEvent } from '../events/accept-blue-subscription-event';

@Injectable()
export class AcceptBlueService implements OnApplicationBootstrap {
Expand Down Expand Up @@ -167,8 +168,9 @@ export class AcceptBlueService implements OnApplicationBootstrap {
`We can only handle Accept Blue payments for logged in users, because we need to save the payment methods on Accept Blue customers`
);
}
const acceptBlueCustomer = await client.getOrCreateCustomer(
order.customer.emailAddress
const acceptBlueCustomer = await client.upsertCustomer(
order.customer.emailAddress,
this.mapToAcceptBlueCustomerInput(order, order.customer)
);
await this.customerService.update(ctx, {
id: order.customer?.id,
Expand Down Expand Up @@ -633,6 +635,49 @@ export class AcceptBlueService implements OnApplicationBootstrap {
return orderLineIds as ID[];
}

/**
* Map a Vendure customer to an Accept Blue customer.
* Uses the order's shipping and billing address as customer address
*/
mapToAcceptBlueCustomerInput(
order: Order,
customer: Customer
): AcceptBlueCustomerInput {
const shippingName = order.shippingAddress?.fullName?.split(' ');
const shippingAddress: AcceptBlueCustomerInput['shipping_info'] = {
first_name: shippingName?.[0] ?? customer.firstName,
last_name: shippingName?.[1] ?? customer.lastName,
street: order.shippingAddress?.streetLine1,
street2: order.shippingAddress?.streetLine2,
zip: order.shippingAddress?.postalCode,
state: order.shippingAddress?.province,
phone: order.shippingAddress?.phoneNumber,
city: order.shippingAddress?.city,
country: order.shippingAddress?.countryCode,
};
const billingName = order.billingAddress?.fullName?.split(' ');
const billingAddress: AcceptBlueCustomerInput['billing_info'] = {
first_name: billingName?.[0] ?? customer.firstName,
last_name: billingName?.[1] ?? customer.lastName,
street: order.billingAddress?.streetLine1,
street2: order.billingAddress?.streetLine2,
zip: order.billingAddress?.postalCode,
state: order.billingAddress?.province,
phone: order.billingAddress?.phoneNumber,
city: order.billingAddress?.city,
country: order.billingAddress?.countryCode,
};
return {
first_name: customer.firstName,
last_name: customer.lastName,
identifier: customer.emailAddress,
email: customer.emailAddress,
shipping_info: shippingAddress,
billing_info: billingAddress,
phone: customer.phoneNumber,
};
}

/**
* Map a subscription from Accept Blue to the GraphQL Subscription type
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/vendure-plugin-accept-blue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface AcceptBlueAddress {
city: string;
zip: string;
country: string;
phone: string;
phone?: string;
}

/** +++++ Transactions +++++ */
Expand Down Expand Up @@ -113,8 +113,8 @@ export interface AcceptBlueCustomerInput {
website?: string;
phone?: string;
alternate_phone?: string;
billing_info?: AcceptBlueAddress;
shipping_info?: AcceptBlueAddress;
billing_info?: Partial<AcceptBlueAddress>;
shipping_info?: Partial<AcceptBlueAddress>;
active?: boolean;
}

Expand Down
Loading

0 comments on commit d86f434

Please sign in to comment.