Skip to content

Commit

Permalink
Merge pull request #155 from jamalsoueidan/activate-customer-through-…
Browse files Browse the repository at this point in the history
…webhook

Activate customer through webhook
  • Loading branch information
jamalsoueidan authored May 28, 2024
2 parents 905e700 + b0f50f1 commit 2bcd2a9
Show file tree
Hide file tree
Showing 69 changed files with 1,149 additions and 772 deletions.
13 changes: 12 additions & 1 deletion src/functions/customer-upload.orchestrators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
updateArticle,
updateArticleName,
} from "./customer/orchestrations/customer/update/update-article";
import {
updateUserMetaobject,
updateUserMetaobjectName,
} from "./customer/orchestrations/customer/update/update-user-metaobject";

df.app.activity("fileCreate", {
handler: fileCreateHandler,
Expand Down Expand Up @@ -68,7 +72,14 @@ df.app.orchestration("upload", function* (context: OrchestrationContext) {
metaobjectId: response.id,
});

return yield context.df.callActivity(
yield context.df.callActivity(
updateUserMetaobjectName,
activityType<typeof updateUserMetaobject>({
user,
})
);

yield context.df.callActivity(
updateArticleName,
activityType<typeof updateArticle>({
user,
Expand Down
18 changes: 16 additions & 2 deletions src/functions/customer/controllers/product-options/add.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { z } from "zod";

import { InvocationContext } from "@azure/functions";
import { _ } from "~/library/handler";
import { GidFormat } from "~/library/zod";
import { CustomerProductOptionsAddOrchestration } from "../../orchestrations/product-options/add";
import { CustomerProductOptionsServiceAdd } from "../../services/product-options/add";

export type CustomerProductOptionsControllerAddRequest = {
query: z.infer<typeof CustomerProductOptionsControllerAddSchema>;
body: z.infer<typeof CustomerProductOptionsControllerAddBodySchema>;
context: InvocationContext;
};

const CustomerProductOptionsControllerAddSchema = z.object({
Expand All @@ -20,15 +23,26 @@ const CustomerProductOptionsControllerAddBodySchema = z.object({
});

export const CustomerProductOptionsControllerAdd = _(
({ query, body }: CustomerProductOptionsControllerAddRequest) => {
async ({
query,
body,
context,
}: CustomerProductOptionsControllerAddRequest) => {
const validateQuery =
CustomerProductOptionsControllerAddSchema.parse(query);
const validateBody =
CustomerProductOptionsControllerAddBodySchema.parse(body);

return CustomerProductOptionsServiceAdd({
const productOption = await CustomerProductOptionsServiceAdd({
...validateQuery,
...validateBody,
});

await CustomerProductOptionsAddOrchestration(
{ productOptionId: productOption.productId, ...validateQuery },
context
);

return context;
}
);
13 changes: 12 additions & 1 deletion src/functions/customer/controllers/product-options/destroy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { z } from "zod";

import { InvocationContext } from "@azure/functions";
import { _ } from "~/library/handler";
import { GidFormat } from "~/library/zod";
import { CustomerProductOptionsDestroyOrchestration } from "../../orchestrations/product-options/destroy";
import { CustomerProductOptionsServiceDestroy } from "../../services/product-options/destroy";

export type CustomerProductOptionsControllerDestroyRequest = {
query: z.infer<typeof CustomerProductOptionsControllerDestroySchema>;
context: InvocationContext;
};

const CustomerProductOptionsControllerDestroySchema = z.object({
Expand All @@ -15,10 +18,18 @@ const CustomerProductOptionsControllerDestroySchema = z.object({
});

export const CustomerProductOptionsControllerDestroy = _(
({ query }: CustomerProductOptionsControllerDestroyRequest) => {
async ({
query,
context,
}: CustomerProductOptionsControllerDestroyRequest) => {
const validateQuery =
CustomerProductOptionsControllerDestroySchema.parse(query);

await CustomerProductOptionsDestroyOrchestration(
{ productOptionId: validateQuery.optionProductId },
context
);

return CustomerProductOptionsServiceDestroy(validateQuery);
}
);
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/product/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ jest.mock("../../orchestrations/product/update", () => ({
}),
}));

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerProductControllerAdd", () => {
let context: InvocationContext;
Expand Down
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/product/destroy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {

require("~/library/jest/mongoose/mongodb.jest");

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerProductControllerDestroy", () => {
let context: InvocationContext;
Expand Down
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/schedule/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {

require("~/library/jest/mongoose/mongodb.jest");

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerScheduleControllerCreate", () => {
let context: InvocationContext;
Expand Down
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/schedule/destroy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import {

require("~/library/jest/mongoose/mongodb.jest");

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerScheduleControllerDestroy", () => {
let context: InvocationContext;
Expand Down
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/schedule/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {

require("~/library/jest/mongoose/mongodb.jest");

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerScheduleControllerUpdate", () => {
let context: InvocationContext;
Expand Down
6 changes: 3 additions & 3 deletions src/functions/customer/controllers/slot/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import {

require("~/library/jest/mongoose/mongodb.jest");

jest.mock("@shopify/admin-api-client", () => ({
createAdminApiClient: () => ({
jest.mock("~/library/shopify", () => ({
shopifyAdmin: jest.fn().mockReturnValue({
request: jest.fn(),
}),
}));

const mockRequest = shopifyAdmin.request as jest.Mock;
const mockRequest = shopifyAdmin().request as jest.Mock;

describe("CustomerScheduleSlotControllerUpdate", () => {
let context: InvocationContext;
Expand Down
2 changes: 1 addition & 1 deletion src/functions/customer/controllers/upload/file-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { shopifyAdmin } from "~/library/shopify";
import { FileInputProps, getFilenameFromUrl } from "./types";

export async function fileCreateHandler(input: FileInputProps) {
const { data } = await shopifyAdmin.request(FILE_CREATE, {
const { data } = await shopifyAdmin().request(FILE_CREATE, {
variables: {
files: {
alt: getFilenameFromUrl(input.resourceUrl),
Expand Down
2 changes: 1 addition & 1 deletion src/functions/customer/controllers/upload/file-get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { shopifyAdmin } from "~/library/shopify";

export async function fileGetHandler(metaobjectId: string) {
const { data } = await shopifyAdmin.request(FILE_GET, {
const { data } = await shopifyAdmin().request(FILE_GET, {
variables: {
id: metaobjectId,
},
Expand Down
2 changes: 1 addition & 1 deletion src/functions/customer/controllers/upload/resource-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const CustomerUploadControllerResourceURL = _(
async ({ query }: CustomerUploadControllerResourceURLRequest) => {
const validateData =
CustomerUploadControllerResourceURLQuerySchema.parse(query);
const { data } = await shopifyAdmin.request(UPLOAD_CREATE, {
const { data } = await shopifyAdmin().request(UPLOAD_CREATE, {
variables: {
input: [
{
Expand Down
59 changes: 33 additions & 26 deletions src/functions/customer/orchestrations/customer/create.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { InvocationContext } from "@azure/functions";
import { addSeconds } from "date-fns";
import * as df from "durable-functions";
import { OrchestrationContext } from "durable-functions";
import { User } from "~/functions/user";
import { activityType } from "~/library/orchestration";
import { createArticle } from "./create/create-article";
import { createCollection } from "./create/create-collection";
import { createUserMetaobject } from "./create/create-user-metaobject";
import { publishCollection } from "./create/publish-collection";
import { updateUser } from "./create/update-user";
import { createArticle, createArticleName } from "./create/create-article";
import {
createCollection,
createCollectionName,
} from "./create/create-collection";
import {
createUserMetaobject,
createUserMetaobjectName,
} from "./create/create-user-metaobject";
import {
publishCollection,
publishCollectionName,
} from "./create/publish-collection";
import {
updateUserMetafields,
updateUserMetafieldsName,
} from "./create/update-user";

const createCollectionName = "createCollection";
df.app.activity(createCollectionName, { handler: createCollection });

const createUserMetaobjectName = "createUserMetaobject";
df.app.activity(createUserMetaobjectName, { handler: createUserMetaobject });

const createArticleName = "createArticle";
df.app.activity(createArticleName, { handler: createArticle });

const publishCollectionName = "publishCollection";
df.app.activity(publishCollectionName, { handler: publishCollection });

const updateUserName = "updateUser";
df.app.activity(updateUserName, { handler: updateUser });
df.app.activity(updateUserMetafieldsName, { handler: updateUserMetafields });

const orchestrator: df.OrchestrationHandler = function* (
context: OrchestrationContext
Expand Down Expand Up @@ -58,25 +62,28 @@ const orchestrator: df.OrchestrationHandler = function* (
})
);

const publish: Awaited<ReturnType<typeof publishCollection>> =
const userUpdated: Awaited<ReturnType<typeof updateUserMetafields>> =
yield context.df.callActivity(
publishCollectionName,
activityType<typeof publishCollection>({
collectionId: collectionMetaobject.id,
})
);

const userUpdated: Awaited<ReturnType<typeof updateUser>> =
yield context.df.callActivity(
updateUserName,
activityType<typeof updateUser>({
updateUserMetafieldsName,
activityType<typeof updateUserMetafields>({
collectionMetaobjectId: collectionMetaobject.id,
userId: user._id,
userMetaobjectId: userMetaobject.id,
articleId: article.article.id,
})
);

const nextExecution = addSeconds(context.df.currentUtcDateTime, 5);
yield context.df.createTimer(nextExecution);

const publish: Awaited<ReturnType<typeof publishCollection>> =
yield context.df.callActivity(
publishCollectionName,
activityType<typeof publishCollection>({
collectionId: collectionMetaobject.id,
})
);

return {
collectionMetaobject,
userMetaobject,
Expand Down
Loading

0 comments on commit 2bcd2a9

Please sign in to comment.