@shopnex/cj-plugin 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { setTenantCredentials } from "./sdk/access-token";
|
|
2
2
|
import { createOrderHook } from "./service/create-order.hook";
|
|
3
3
|
import { CjCollection } from "./CjCollection";
|
|
4
|
+
import pkg from "../package.json";
|
|
4
5
|
export const cjPlugin = (pluginOptions)=>(config)=>{
|
|
5
6
|
const isEnabled = pluginOptions.isEnabled ?? true;
|
|
6
7
|
const ordersCollection = config.collections?.find((collection)=>collection.slug === (pluginOptions.orderCollectionSlug || "orders"));
|
|
@@ -17,8 +18,14 @@ export const cjPlugin = (pluginOptions)=>(config)=>{
|
|
|
17
18
|
overrides: pluginOptions.collectionOverrides
|
|
18
19
|
}));
|
|
19
20
|
const productCollection = config.collections?.find((collection)=>collection.slug === "products");
|
|
20
|
-
const
|
|
21
|
-
|
|
21
|
+
const orderCollection = config.collections?.find((collection)=>collection.slug === "orders");
|
|
22
|
+
const productSourceField = productCollection?.fields?.find((field)=>field.name === "source");
|
|
23
|
+
const orderSourceField = orderCollection?.fields?.find((field)=>field.name === "source");
|
|
24
|
+
productSourceField.options.push({
|
|
25
|
+
label: "CJ",
|
|
26
|
+
value: "cj"
|
|
27
|
+
});
|
|
28
|
+
orderSourceField.options.push({
|
|
22
29
|
label: "CJ",
|
|
23
30
|
value: "cj"
|
|
24
31
|
});
|
|
@@ -33,6 +40,15 @@ export const cjPlugin = (pluginOptions)=>(config)=>{
|
|
|
33
40
|
if (incomingOnInit) {
|
|
34
41
|
await incomingOnInit(payload);
|
|
35
42
|
}
|
|
43
|
+
await config.custom?.syncPlugin?.(payload, {
|
|
44
|
+
name: pkg.name,
|
|
45
|
+
version: pkg.version,
|
|
46
|
+
description: pkg.description,
|
|
47
|
+
license: pkg.license,
|
|
48
|
+
author: pkg.author,
|
|
49
|
+
icon: pkg.icon,
|
|
50
|
+
category: pkg.category
|
|
51
|
+
});
|
|
36
52
|
const cjSettingsDocs = [];
|
|
37
53
|
const cjSettings = await payload.find({
|
|
38
54
|
collection: "cj-settings"
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config, SelectField } from \"payload\";\nimport { setTenantCredentials } from \"./sdk/access-token\";\nimport { createOrderHook } from \"./service/create-order.hook\";\nimport { CjCollectionProps, CjCollection } from \"./CjCollection\";\n\ninterface PluginOptions {\n isEnabled?: boolean;\n cjApiKey: string;\n cjEmailAddress: string;\n cjRefreshToken?: string;\n collectionOverrides?: CjCollectionProps[\"overrides\"];\n orderCollectionSlug?: string;\n}\n\nexport const cjPlugin =\n (pluginOptions: PluginOptions) =>\n (config: Config): Config => {\n const isEnabled = pluginOptions.isEnabled ?? true;\n\n const ordersCollection = config.collections?.find(\n (collection) =>\n collection.slug ===\n (pluginOptions.orderCollectionSlug || \"orders\")\n );\n\n if (!ordersCollection) {\n throw new Error(\"No orders collection found\");\n }\n\n if (!ordersCollection.hooks) {\n ordersCollection.hooks = {};\n }\n\n if (!ordersCollection.hooks?.afterChange?.length) {\n ordersCollection.hooks.afterChange = [];\n }\n config.collections?.push(\n CjCollection({\n overrides: pluginOptions.collectionOverrides,\n })\n );\n const productCollection = config.collections?.find(\n (collection) => collection.slug === \"products\"\n );\n\n const
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config, SelectField } from \"payload\";\nimport { setTenantCredentials } from \"./sdk/access-token\";\nimport { createOrderHook } from \"./service/create-order.hook\";\nimport { CjCollectionProps, CjCollection } from \"./CjCollection\";\nimport pkg from \"../package.json\";\n\ninterface PluginOptions {\n isEnabled?: boolean;\n cjApiKey: string;\n cjEmailAddress: string;\n cjRefreshToken?: string;\n collectionOverrides?: CjCollectionProps[\"overrides\"];\n orderCollectionSlug?: string;\n}\n\nexport const cjPlugin =\n (pluginOptions: PluginOptions) =>\n (config: Config): Config => {\n const isEnabled = pluginOptions.isEnabled ?? true;\n\n const ordersCollection = config.collections?.find(\n (collection) =>\n collection.slug ===\n (pluginOptions.orderCollectionSlug || \"orders\")\n );\n\n if (!ordersCollection) {\n throw new Error(\"No orders collection found\");\n }\n\n if (!ordersCollection.hooks) {\n ordersCollection.hooks = {};\n }\n\n if (!ordersCollection.hooks?.afterChange?.length) {\n ordersCollection.hooks.afterChange = [];\n }\n config.collections?.push(\n CjCollection({\n overrides: pluginOptions.collectionOverrides,\n })\n );\n const productCollection = config.collections?.find(\n (collection) => collection.slug === \"products\"\n );\n\n const orderCollection = config.collections?.find(\n (collection) => collection.slug === \"orders\"\n );\n\n const productSourceField = productCollection?.fields?.find(\n (field) => (field as SelectField).name === \"source\"\n ) as SelectField;\n\n const orderSourceField = orderCollection?.fields?.find(\n (field) => (field as SelectField).name === \"source\"\n ) as SelectField;\n\n productSourceField.options.push({\n label: \"CJ\",\n value: \"cj\",\n });\n\n orderSourceField.options.push({\n label: \"CJ\",\n value: \"cj\",\n });\n\n if (!isEnabled) {\n return config;\n }\n if (ordersCollection.hooks?.afterChange) {\n ordersCollection.hooks.afterChange.push(createOrderHook);\n }\n\n const incomingOnInit = config.onInit;\n\n config.onInit = async (payload) => {\n if (incomingOnInit) {\n await incomingOnInit(payload);\n }\n\n await config.custom?.syncPlugin?.(payload, {\n name: pkg.name,\n version: pkg.version,\n description: pkg.description,\n license: pkg.license,\n author: pkg.author,\n icon: pkg.icon,\n category: pkg.category,\n });\n\n const cjSettingsDocs: any = [];\n\n const cjSettings = await payload.find({\n collection: \"cj-settings\" as any,\n });\n\n cjSettingsDocs.push(...cjSettings?.docs);\n\n cjSettingsDocs.forEach((config: any) => {\n setTenantCredentials(config?.shop?.slug || \"1\", {\n emailAddress: config.email || process.env.CJ_EMAIL_ADDRESS,\n password: config.apiToken || process.env.CJ_PASSWORD,\n });\n });\n };\n\n return config;\n };\n"],"names":["setTenantCredentials","createOrderHook","CjCollection","pkg","cjPlugin","pluginOptions","config","isEnabled","ordersCollection","collections","find","collection","slug","orderCollectionSlug","Error","hooks","afterChange","length","push","overrides","collectionOverrides","productCollection","orderCollection","productSourceField","fields","field","name","orderSourceField","options","label","value","incomingOnInit","onInit","payload","custom","syncPlugin","version","description","license","author","icon","category","cjSettingsDocs","cjSettings","docs","forEach","shop","emailAddress","email","process","env","CJ_EMAIL_ADDRESS","password","apiToken","CJ_PASSWORD"],"mappings":"AACA,SAASA,oBAAoB,QAAQ,qBAAqB;AAC1D,SAASC,eAAe,QAAQ,8BAA8B;AAC9D,SAA4BC,YAAY,QAAQ,iBAAiB;AACjE,OAAOC,SAAS,kBAAkB;AAWlC,OAAO,MAAMC,WACT,CAACC,gBACD,CAACC;QACG,MAAMC,YAAYF,cAAcE,SAAS,IAAI;QAE7C,MAAMC,mBAAmBF,OAAOG,WAAW,EAAEC,KACzC,CAACC,aACGA,WAAWC,IAAI,KACdP,CAAAA,cAAcQ,mBAAmB,IAAI,QAAO;QAGrD,IAAI,CAACL,kBAAkB;YACnB,MAAM,IAAIM,MAAM;QACpB;QAEA,IAAI,CAACN,iBAAiBO,KAAK,EAAE;YACzBP,iBAAiBO,KAAK,GAAG,CAAC;QAC9B;QAEA,IAAI,CAACP,iBAAiBO,KAAK,EAAEC,aAAaC,QAAQ;YAC9CT,iBAAiBO,KAAK,CAACC,WAAW,GAAG,EAAE;QAC3C;QACAV,OAAOG,WAAW,EAAES,KAChBhB,aAAa;YACTiB,WAAWd,cAAce,mBAAmB;QAChD;QAEJ,MAAMC,oBAAoBf,OAAOG,WAAW,EAAEC,KAC1C,CAACC,aAAeA,WAAWC,IAAI,KAAK;QAGxC,MAAMU,kBAAkBhB,OAAOG,WAAW,EAAEC,KACxC,CAACC,aAAeA,WAAWC,IAAI,KAAK;QAGxC,MAAMW,qBAAqBF,mBAAmBG,QAAQd,KAClD,CAACe,QAAU,AAACA,MAAsBC,IAAI,KAAK;Q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|
2
|
-
|
|
3
|
-
|
|
4
|
-
items: Array<{
|
|
5
|
-
productUrl: string;
|
|
6
|
-
quantity: number;
|
|
7
|
-
variant: {
|
|
8
|
-
variantId: string;
|
|
9
|
-
};
|
|
10
|
-
}>;
|
|
11
|
-
}
|
|
12
|
-
export declare const createOrderHook: CollectionAfterChangeHook<Orders>;
|
|
1
|
+
import type { CollectionAfterChangeHook } from "payload";
|
|
2
|
+
import { Order } from "@shopnex/types";
|
|
3
|
+
export declare const createOrderHook: CollectionAfterChangeHook<Order>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cjSdk } from "../sdk/cj-sdk";
|
|
2
2
|
export const createOrderHook = async ({ doc, req })=>{
|
|
3
|
-
if (doc.orderStatus !== "processing") {
|
|
3
|
+
if (doc.orderStatus !== "processing" || doc.source !== "cj") {
|
|
4
4
|
return;
|
|
5
5
|
}
|
|
6
6
|
const payload = req.payload;
|
|
@@ -8,7 +8,7 @@ export const createOrderHook = async ({ doc, req })=>{
|
|
|
8
8
|
collection: "cj-settings",
|
|
9
9
|
where: {
|
|
10
10
|
shop: {
|
|
11
|
-
equals: doc.
|
|
11
|
+
equals: doc.shop?.id
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
});
|
|
@@ -35,10 +35,10 @@ export const createOrderHook = async ({ doc, req })=>{
|
|
|
35
35
|
logisticName: "CJPacket Liquid US",
|
|
36
36
|
orderNumber: doc.orderId,
|
|
37
37
|
payType: 2,
|
|
38
|
-
products: doc.items.map((item)=>({
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
// products: doc.items.map((item) => ({
|
|
39
|
+
// quantity: item.quantity,
|
|
40
|
+
// vid: item.variant.variantId,
|
|
41
|
+
// })),
|
|
42
42
|
podProperties,
|
|
43
43
|
remark: "",
|
|
44
44
|
shippingAddress: doc.shippingAddress?.address?.line1 || "",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/service/create-order.hook.ts"],"sourcesContent":["import type { BasePayload, CollectionAfterChangeHook, Document } from \"payload\";\nimport { cjSdk } from \"../sdk/cj-sdk\";\
|
|
1
|
+
{"version":3,"sources":["../../src/service/create-order.hook.ts"],"sourcesContent":["import type { BasePayload, CollectionAfterChangeHook, Document } from \"payload\";\nimport { cjSdk } from \"../sdk/cj-sdk\";\nimport { Order, Shop } from \"@shopnex/types\";\n\nexport const createOrderHook: CollectionAfterChangeHook<Order> = async ({\n doc,\n req,\n}) => {\n if (doc.orderStatus !== \"processing\" || doc.source !== \"cj\") {\n return;\n }\n const payload: BasePayload = req.payload;\n const cjSettings = await payload.find({\n collection: \"cj-settings\" as any,\n where: {\n shop: {\n equals: (doc.shop as Shop)?.id,\n },\n },\n });\n const cjConfig: any = cjSettings?.docs[0];\n const podProperties = cjConfig?.pod?.url\n ? [\n {\n areaName: \"LogoArea\",\n links: [cjConfig?.pod?.url],\n type: \"1\",\n layer: [],\n },\n ]\n : [];\n const sdk = cjSdk({ accessToken: cjConfig?.accessToken });\n const result = await sdk.orders.createOrder({\n consigneeID: doc.billingAddress?.name || \"\",\n email: doc.billingAddress?.email || \"\",\n fromCountryCode: \"CN\",\n houseNumber: doc.shippingAddress?.address?.line2 || \"\",\n iossType: 1,\n logisticName: \"CJPacket Liquid US\",\n orderNumber: doc.orderId,\n payType: 2,\n // products: doc.items.map((item) => ({\n // quantity: item.quantity,\n // vid: item.variant.variantId,\n // })),\n podProperties,\n remark: \"\",\n shippingAddress: doc.shippingAddress?.address?.line1 || \"\",\n shippingAddress2: doc.shippingAddress?.address?.line2 || \"\",\n shippingCity: doc.shippingAddress?.address?.city || \"\",\n shippingCountry: doc.shippingAddress?.address?.country || \"\",\n shippingCountryCode: doc.shippingAddress?.address?.country || \"\",\n shippingCounty: doc.shippingAddress?.address?.city || \"\",\n shippingCustomerName: doc.shippingAddress?.name || \"\",\n shippingPhone: doc.shippingAddress?.phone || \"+9999999999\",\n shippingProvince: doc.shippingAddress?.address?.state || \"\",\n shippingZip: doc.shippingAddress?.address?.postal_code || \"\",\n taxId: \"\",\n });\n\n const orderResult = await payload.update({\n collection: \"orders\" as any,\n data: {\n orderStatus: \"shipped\",\n },\n where: {\n id: {\n equals: doc.orderId,\n },\n },\n });\n\n return {\n cjResult: result,\n orderResult,\n };\n};\n"],"names":["cjSdk","createOrderHook","doc","req","orderStatus","source","payload","cjSettings","find","collection","where","shop","equals","id","cjConfig","docs","podProperties","pod","url","areaName","links","type","layer","sdk","accessToken","result","orders","createOrder","consigneeID","billingAddress","name","email","fromCountryCode","houseNumber","shippingAddress","address","line2","iossType","logisticName","orderNumber","orderId","payType","remark","line1","shippingAddress2","shippingCity","city","shippingCountry","country","shippingCountryCode","shippingCounty","shippingCustomerName","shippingPhone","phone","shippingProvince","state","shippingZip","postal_code","taxId","orderResult","update","data","cjResult"],"mappings":"AACA,SAASA,KAAK,QAAQ,gBAAgB;AAGtC,OAAO,MAAMC,kBAAoD,OAAO,EACpEC,GAAG,EACHC,GAAG,EACN;IACG,IAAID,IAAIE,WAAW,KAAK,gBAAgBF,IAAIG,MAAM,KAAK,MAAM;QACzD;IACJ;IACA,MAAMC,UAAuBH,IAAIG,OAAO;IACxC,MAAMC,aAAa,MAAMD,QAAQE,IAAI,CAAC;QAClCC,YAAY;QACZC,OAAO;YACHC,MAAM;gBACFC,QAASV,IAAIS,IAAI,EAAWE;YAChC;QACJ;IACJ;IACA,MAAMC,WAAgBP,YAAYQ,IAAI,CAAC,EAAE;IACzC,MAAMC,gBAAgBF,UAAUG,KAAKC,MAC/B;QACI;YACIC,UAAU;YACVC,OAAO;gBAACN,UAAUG,KAAKC;aAAI;YAC3BG,MAAM;YACNC,OAAO,EAAE;QACb;KACH,GACD,EAAE;IACR,MAAMC,MAAMvB,MAAM;QAAEwB,aAAaV,UAAUU;IAAY;IACvD,MAAMC,SAAS,MAAMF,IAAIG,MAAM,CAACC,WAAW,CAAC;QACxCC,aAAa1B,IAAI2B,cAAc,EAAEC,QAAQ;QACzCC,OAAO7B,IAAI2B,cAAc,EAAEE,SAAS;QACpCC,iBAAiB;QACjBC,aAAa/B,IAAIgC,eAAe,EAAEC,SAASC,SAAS;QACpDC,UAAU;QACVC,cAAc;QACdC,aAAarC,IAAIsC,OAAO;QACxBC,SAAS;QACT,uCAAuC;QACvC,+BAA+B;QAC/B,mCAAmC;QACnC,OAAO;QACPzB;QACA0B,QAAQ;QACRR,iBAAiBhC,IAAIgC,eAAe,EAAEC,SAASQ,SAAS;QACxDC,kBAAkB1C,IAAIgC,eAAe,EAAEC,SAASC,SAAS;QACzDS,cAAc3C,IAAIgC,eAAe,EAAEC,SAASW,QAAQ;QACpDC,iBAAiB7C,IAAIgC,eAAe,EAAEC,SAASa,WAAW;QAC1DC,qBAAqB/C,IAAIgC,eAAe,EAAEC,SAASa,WAAW;QAC9DE,gBAAgBhD,IAAIgC,eAAe,EAAEC,SAASW,QAAQ;QACtDK,sBAAsBjD,IAAIgC,eAAe,EAAEJ,QAAQ;QACnDsB,eAAelD,IAAIgC,eAAe,EAAEmB,SAAS;QAC7CC,kBAAkBpD,IAAIgC,eAAe,EAAEC,SAASoB,SAAS;QACzDC,aAAatD,IAAIgC,eAAe,EAAEC,SAASsB,eAAe;QAC1DC,OAAO;IACX;IAEA,MAAMC,cAAc,MAAMrD,QAAQsD,MAAM,CAAC;QACrCnD,YAAY;QACZoD,MAAM;YACFzD,aAAa;QACjB;QACAM,OAAO;YACHG,IAAI;gBACAD,QAAQV,IAAIsC,OAAO;YACvB;QACJ;IACJ;IAEA,OAAO;QACHsB,UAAUrC;QACVkC;IACJ;AACJ,EAAE"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopnex/cj-plugin",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "A blank template to get started with Payload 3.0",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "ShopNex <support@shopnex.ai> (https://shopnex.ai)",
|
|
6
8
|
"exports": {
|
|
7
9
|
".": {
|
|
8
10
|
"import": "./dist/index.js",
|
|
@@ -33,12 +35,14 @@
|
|
|
33
35
|
"decimal.js": "^10.5.0",
|
|
34
36
|
"jsdom": "^26.0.0",
|
|
35
37
|
"lexical": "0.28.0",
|
|
36
|
-
"@shopnex/
|
|
37
|
-
"@shopnex/
|
|
38
|
+
"@shopnex/types": "0.0.3",
|
|
39
|
+
"@shopnex/utils": "1.0.6"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@types/jsdom": "^21.1.7"
|
|
41
43
|
},
|
|
44
|
+
"icon": "https://cdn.shopnex.ai/shopnex-assets/cj-plugin-icon.svg",
|
|
45
|
+
"category": "Integrations",
|
|
42
46
|
"scripts": {
|
|
43
47
|
"build": "pnpm copyfiles && pnpm build:types && pnpm build:swc",
|
|
44
48
|
"build:swc": "swc ./src -d ./dist --config-file .swcrc --strip-leading-paths",
|