Skip to content

Commit

Permalink
Rename functions, create service for fulfillment
Browse files Browse the repository at this point in the history
  • Loading branch information
soniaklimas committed Nov 27, 2024
1 parent 019be81 commit 5b5bf1f
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";

import { paths } from "@/lib/paths";
import { checkoutService } from "@/services";
import { fulfillmentService } from "@/services";

import type { FormSchema } from "./schema";

Expand All @@ -15,7 +15,7 @@ export async function returnProducts(data: FormSchema, orderId: string) {
quantity: 0, // despite 0 correct qty from order is returned
}));

const response = await checkoutService.orderReturnProducts({
const response = await fulfillmentService.fulfillmentReturnProducts({
order: orderId,
input: { orderLines: selectedOrderLines },
});
Expand Down
5 changes: 5 additions & 0 deletions apps/storefront/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { saleorAddressService } from "@nimara/infrastructure/public/saleor/addre
import { saleorAuthService } from "@nimara/infrastructure/public/saleor/auth/index";
import { saleorCartService } from "@nimara/infrastructure/public/saleor/cart/index";
import { saleorCheckoutService } from "@nimara/infrastructure/public/saleor/checkout/service";
import { saleorFulfillmentService } from "@nimara/infrastructure/public/saleor/fulfillment/service";
import { saleorStoreService } from "@nimara/infrastructure/public/saleor/store/index";
import { saleorUserService } from "@nimara/infrastructure/public/saleor/user/index";
import { stripePaymentService } from "@nimara/infrastructure/public/stripe/payment/index";
Expand All @@ -14,6 +15,10 @@ import { serverEnvs } from "@/envs/server";

export const checkoutService = saleorCheckoutService({
apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL,
});

export const fulfillmentService = saleorFulfillmentService({
apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL,
appToken: serverEnvs.SALEOR_APP_TOKEN,
});

Expand Down
30 changes: 0 additions & 30 deletions packages/infrastructure/src/graphql/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DocumentTypeDecoration } from "@graphql-typed-document-node/core";
import { invariant } from "ts-invariant";

import type { Err, Result } from "@nimara/domain/objects/Error";

Expand All @@ -26,35 +25,6 @@ type NextFetchOptions = { next?: { revalidate?: number; tags?: string[] } };
export type FetchOptions = Omit<RequestInit, "method" | "body"> &
NextFetchOptions;

export const secureSaleorClient = ({
apiURL,
appToken,
}: {
apiURL: string;
appToken: string;
}) => {
const client = graphqlClient(apiURL);

invariant(
appToken,
"Please provide a valid appToken to use secureSaleorClient.",
);

const execute: (typeof client)["execute"] = (query, opts) =>
client.execute(query, {
...opts,
options: {
...opts?.options,
headers: {
authorization: `Bearer ${appToken}`,
...opts?.options?.headers,
},
},
});

return { execute };
};

export const graphqlClient = (
url: RequestInfo | URL,
accessToken?: string | null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,6 @@ export type CheckoutShippingAddressUpdateVariables = Types.Exact<{

export type CheckoutShippingAddressUpdate = CheckoutShippingAddressUpdate_Mutation;

export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_returnFulfillment_Fulfillment = { status: Types.FulfillmentStatus };

export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_errors_OrderError = { field: string | null, message: string | null, code: Types.OrderErrorCode };

export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts = { returnFulfillment: FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_returnFulfillment_Fulfillment | null, errors: Array<FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_errors_OrderError> };

export type FulfillmentReturnProducts_Mutation = { orderFulfillmentReturnProducts: FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts | null };


export type FulfillmentReturnProductsVariables = Types.Exact<{
order: Types.Scalars['ID']['input'];
input: Types.OrderReturnProductsInput;
}>;


export type FulfillmentReturnProducts = FulfillmentReturnProducts_Mutation;

export class TypedDocumentString<TResult, TVariables>
extends String
implements DocumentTypeDecoration<TResult, TVariables>
Expand Down Expand Up @@ -298,18 +281,4 @@ export const CheckoutShippingAddressUpdateDocument = new TypedDocumentString(`
lines
message
variants
}`) as unknown as TypedDocumentString<CheckoutShippingAddressUpdate, CheckoutShippingAddressUpdateVariables>;
export const FulfillmentReturnProductsDocument = new TypedDocumentString(`
mutation FulfillmentReturnProducts($order: ID!, $input: OrderReturnProductsInput!) {
orderFulfillmentReturnProducts(order: $order, input: $input) {
returnFulfillment {
status
}
errors {
field
message
code
}
}
}
`) as unknown as TypedDocumentString<FulfillmentReturnProducts, FulfillmentReturnProductsVariables>;
}`) as unknown as TypedDocumentString<CheckoutShippingAddressUpdate, CheckoutShippingAddressUpdateVariables>;
5 changes: 0 additions & 5 deletions packages/infrastructure/src/public/saleor/checkout/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { checkoutGetUseCase } from "#root/use-cases/checkout/checkout-get-use-ca
import { checkoutRemovePromoCodeUseCase } from "#root/use-cases/checkout/checkout-remove-promo-code-use-case";
import { checkoutShippingAddressUpdateUseCase } from "#root/use-cases/checkout/checkout-shipping-address-update-use-case";
import { orderCreateUseCase } from "#root/use-cases/checkout/order-create-use-case";
import { orderReturnProductsUseCase } from "#root/use-cases/checkout/order-return-products-use-case";

import { saleorCheckoutAddPromoCodeInfra } from "./infrastructure/checkout-add-promo-code-infra";
import { saleorCheckoutCustomerAttachInfra } from "./infrastructure/checkout-customer-attach-infrastructure";
Expand All @@ -17,7 +16,6 @@ import { saleorCheckoutRemovePromoCodeInfra } from "./infrastructure/checkout-re
import { saleorCheckoutShippingAddressUpdateInfra } from "./infrastructure/checkout-shipping-address-update-infra";
import { saleorCheckoutEmailUpdateInfra } from "./infrastructure/checkout-update-email-infra";
import { orderCreateInfra } from "./infrastructure/order-create-infra";
import { orderReturnProductsInfra } from "./infrastructure/order-return-products-infra";
import { saleorCheckoutBillingAddressUpdateInfra } from "./infrastructure/update-checkout-billing-address-infra";
import type { CheckoutService, SaleorCheckoutServiceConfig } from "./types";

Expand Down Expand Up @@ -48,9 +46,6 @@ export const saleorCheckoutService: CheckoutService<
orderCreate: orderCreateUseCase({
orderCreate: orderCreateInfra(serviceConfig),
}),
orderReturnProducts: orderReturnProductsUseCase({
orderReturnProducts: orderReturnProductsInfra(serviceConfig),
}),
checkoutAddPromoCode: checkoutAddPromoCodeUseCase({
checkoutAddPromoCodeInfra: saleorCheckoutAddPromoCodeInfra(serviceConfig),
}),
Expand Down
28 changes: 0 additions & 28 deletions packages/infrastructure/src/public/saleor/checkout/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,6 @@ export type OrderCreateInfra = (opts: { id: string }) => Promise<{
}>;
export type OrderCreateUseCase = OrderCreateInfra;

export type OrderReturnProductsOptions = {
input: {
orderLines: {
orderLineId: string;
quantity: number;
}[];
};
order: string;
};

export type OrderReturnProductsInfra = (
opts: OrderReturnProductsOptions,
) => Promise<
| { isSuccess: true }
| { isSuccess: false }
| {
isSuccess: false;
serverError: BaseError;
}
| {
isSuccess: false;
validationErrors: { code: string; field: string | null }[];
}
>;
export type OrderReturnProductsUseCase = OrderReturnProductsInfra;

export type CheckoutBillingAddressUpdateInfra = (opts: {
address: Partial<Omit<Address, "id">>;
checkoutId: string;
Expand Down Expand Up @@ -153,7 +127,6 @@ export type ValidationError =

export type SaleorCheckoutServiceConfig = {
apiURL: string;
appToken?: string;
};

export type CheckoutService<Config> = (config: Config) => {
Expand All @@ -166,5 +139,4 @@ export type CheckoutService<Config> = (config: Config) => {
checkoutShippingAddressUpdate: CheckoutShippingAddressUpdateUseCase;
deliveryMethodUpdate: CheckoutDeliveryMethodUpdateUseCase;
orderCreate: OrderCreateUseCase;
orderReturnProducts: OrderReturnProductsInfra;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type * as Types from '@nimara/codegen/schema';

import type { DocumentTypeDecoration } from '@graphql-typed-document-node/core';
export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_returnFulfillment_Fulfillment = { status: Types.FulfillmentStatus };

export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_errors_OrderError = { field: string | null, message: string | null, code: Types.OrderErrorCode };

export type FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts = { returnFulfillment: FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_returnFulfillment_Fulfillment | null, errors: Array<FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts_errors_OrderError> };

export type FulfillmentReturnProducts_Mutation = { orderFulfillmentReturnProducts: FulfillmentReturnProducts_orderFulfillmentReturnProducts_FulfillmentReturnProducts | null };


export type FulfillmentReturnProductsVariables = Types.Exact<{
order: Types.Scalars['ID']['input'];
input: Types.OrderReturnProductsInput;
}>;


export type FulfillmentReturnProducts = FulfillmentReturnProducts_Mutation;

export class TypedDocumentString<TResult, TVariables>
extends String
implements DocumentTypeDecoration<TResult, TVariables>
{
__apiType?: DocumentTypeDecoration<TResult, TVariables>['__apiType'];

constructor(private value: string, public __meta__?: Record<string, any>) {
super(value);
}

toString(): string & DocumentTypeDecoration<TResult, TVariables> {
return this.value;
}
}

export const FulfillmentReturnProductsDocument = new TypedDocumentString(`
mutation FulfillmentReturnProducts($order: ID!, $input: OrderReturnProductsInput!) {
orderFulfillmentReturnProducts(order: $order, input: $input) {
returnFulfillment {
status
}
errors {
field
message
code
}
}
}
`) as unknown as TypedDocumentString<FulfillmentReturnProducts, FulfillmentReturnProductsVariables>;
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { BaseError } from "@nimara/domain/objects/Error";

import { secureSaleorClient } from "#root/graphql/client";
import { graphqlClient } from "#root/graphql/client";
import { loggingService } from "#root/logging/service";

import { FulfillmentReturnProductsDocument } from "../graphql/mutations/generated";
import { FulfillmentReturnProductsDocument } from "../grapqhql/mutations/generated";
import type {
OrderReturnProductsInfra,
SaleorCheckoutServiceConfig,
FulfillmentReturnProductsInfra,
SaleorFulfillmentServiceConfig,
} from "../types";

export const orderReturnProductsInfra =
export const saleorFulfillmentReturnProductsInfra =
({
apiURL,
appToken = "",
}: SaleorCheckoutServiceConfig): OrderReturnProductsInfra =>
}: SaleorFulfillmentServiceConfig): FulfillmentReturnProductsInfra =>
async ({ order, input }) => {
const { data, error } = await secureSaleorClient({
apiURL,
appToken,
}).execute(FulfillmentReturnProductsDocument, {
variables: { order, input },
});
const { data, error } = await graphqlClient(apiURL, appToken).execute(
FulfillmentReturnProductsDocument,
{
variables: { order, input },
},
);

if (error) {
loggingService.error("Failed to return products", error);
Expand All @@ -31,17 +31,17 @@ export const orderReturnProductsInfra =
};
}

const isReturned =
data?.orderFulfillmentReturnProducts?.returnFulfillment?.status ===
"RETURNED";

if (data?.orderFulfillmentReturnProducts?.errors?.length) {
return {
isSuccess: false,
validationErrors: data.orderFulfillmentReturnProducts.errors,
};
}

const isReturned =
data?.orderFulfillmentReturnProducts?.returnFulfillment?.status ===
"RETURNED";

if (!isReturned) {
return { isSuccess: false };
}
Expand Down
16 changes: 16 additions & 0 deletions packages/infrastructure/src/public/saleor/fulfillment/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fulfillmentReturnProductsUseCase } from "#root/use-cases/fulfillment/fulfillment-return-products-use-case";

import { saleorFulfillmentReturnProductsInfra } from "../fulfillment/infrastructure/fulfillment-return-products-infra";
import type {
FulfillmentService,
SaleorFulfillmentServiceConfig,
} from "./types";

export const saleorFulfillmentService: FulfillmentService<
SaleorFulfillmentServiceConfig
> = (serviceConfig) => ({
fulfillmentReturnProducts: fulfillmentReturnProductsUseCase({
fulfillmentReturnProducts:
saleorFulfillmentReturnProductsInfra(serviceConfig),
}),
});
36 changes: 36 additions & 0 deletions packages/infrastructure/src/public/saleor/fulfillment/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { BaseError } from "@nimara/domain/objects/Error";

export type FulfillmentReturnProductsOptions = {
input: {
orderLines: {
orderLineId: string;
quantity: number;
}[];
};
order: string;
};

export type FulfillmentReturnProductsInfra = (
opts: FulfillmentReturnProductsOptions,
) => Promise<
| { isSuccess: true }
| { isSuccess: false }
| {
isSuccess: false;
serverError: BaseError;
}
| {
isSuccess: false;
validationErrors: { code: string; field: string | null }[];
}
>;
export type FulfillmentReturnProductsUseCase = FulfillmentReturnProductsInfra;

export type SaleorFulfillmentServiceConfig = {
apiURL: string;
appToken: string;
};

export type FulfillmentService<Config> = (config: Config) => {
fulfillmentReturnProducts: FulfillmentReturnProductsInfra;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {
FulfillmentReturnProductsInfra,
FulfillmentReturnProductsOptions,
FulfillmentReturnProductsUseCase,
} from "#root/public/saleor/fulfillment/types";

export const fulfillmentReturnProductsUseCase =
({
fulfillmentReturnProducts,
}: {
fulfillmentReturnProducts: FulfillmentReturnProductsInfra;
}): FulfillmentReturnProductsUseCase =>
async (opts: FulfillmentReturnProductsOptions) => {
return fulfillmentReturnProducts(opts);
};

0 comments on commit 5b5bf1f

Please sign in to comment.