@shopnex/cj-plugin 1.0.3 → 1.0.4

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.
@@ -1,2 +1,9 @@
1
- import type { GlobalConfig } from "payload";
2
- export declare const CjSettings: GlobalConfig;
1
+ import type { CollectionConfig, GlobalConfig } from "payload";
2
+ export type CjCollectionProps = {
3
+ overrides?: Partial<CollectionConfig>;
4
+ };
5
+ export type CjGlobalProps = {
6
+ overrides?: Partial<GlobalConfig>;
7
+ };
8
+ export declare const CjSettings: ({ overrides }: CjGlobalProps) => GlobalConfig;
9
+ export declare const CjConfigCollection: ({ overrides }: CjCollectionProps) => CollectionConfig;
@@ -1,90 +1,99 @@
1
1
  import { syncProducts } from "./service/sync-products";
2
- import { decryptToken, encryptToken } from "./util/manage-tokens";
3
- export const CjSettings = {
4
- slug: "cj-settings",
5
- access: {
6
- read: ()=>true
7
- },
8
- admin: {
9
- group: "Plugins"
10
- },
11
- fields: [
12
- {
13
- label: "Credentials",
14
- type: "collapsible",
15
- fields: [
16
- {
17
- type: "row",
18
- fields: [
19
- {
20
- name: "emailAddress",
21
- type: "text"
22
- },
23
- {
24
- name: "apiToken",
25
- type: "text",
26
- admin: {
27
- components: {
28
- Field: "@shopnex/cj-plugin/rsc#ApiToken"
29
- }
30
- }
31
- }
32
- ]
33
- }
34
- ]
2
+ // Shared fields definition
3
+ const sharedFields = [
4
+ // {
5
+ // label: "Credentials",
6
+ // type: "collapsible",
7
+ // fields: [
8
+ // {
9
+ // type: "row",
10
+ // fields: [
11
+ // {
12
+ // name: "emailAddress",
13
+ // type: "text",
14
+ // },
15
+ // {
16
+ // name: "apiToken",
17
+ // type: "text",
18
+ // admin: {
19
+ // components: {
20
+ // Field: "@shopnex/cj-plugin/rsc#ApiToken",
21
+ // },
22
+ // },
23
+ // },
24
+ // ],
25
+ // },
26
+ // ],
27
+ // },
28
+ // {
29
+ // label: "Logo Area POD",
30
+ // name: "pod",
31
+ // type: "upload",
32
+ // relationTo: "media",
33
+ // },
34
+ {
35
+ name: "items",
36
+ type: "array",
37
+ admin: {
38
+ description: "A list of product URLs to sync with CJ Dropshipping"
35
39
  },
36
- {
37
- label: "Logo Area POD",
38
- name: "pod",
39
- type: "upload",
40
- relationTo: "media"
41
- },
42
- {
43
- name: "items",
44
- type: "array",
45
- admin: {
46
- description: "A list of product URLs to sync with CJ Dropshipping"
47
- },
48
- fields: [
49
- {
50
- name: "productUrl",
51
- type: "text"
52
- }
53
- ],
54
- label: "Products",
55
- labels: {
56
- plural: "Product URLs",
57
- singular: "Product URL"
58
- }
59
- }
60
- ],
61
- hooks: {
62
- beforeChange: [
63
- ({ data })=>{
64
- const tokenToEncrypt = data.apiToken;
65
- const encryptedToken = encryptToken(tokenToEncrypt);
66
- data.apiToken = encryptedToken;
40
+ fields: [
41
+ {
42
+ name: "productUrl",
43
+ type: "text"
67
44
  }
68
45
  ],
69
- beforeRead: [
70
- ({ doc })=>{
71
- doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);
72
- }
73
- ],
74
- afterChange: [
75
- async ({ doc, req: { payload } })=>{
76
- const productIds = doc.items.map((item)=>{
77
- const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\.html)/);
78
- return match ? match[0] : null;
79
- });
80
- await syncProducts(productIds, payload);
81
- },
82
- ({ doc })=>{
83
- doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);
84
- }
85
- ]
86
- },
87
- label: "CJ Dropshipping"
46
+ label: "Products",
47
+ labels: {
48
+ plural: "Product URLs",
49
+ singular: "Product URL"
50
+ }
51
+ }
52
+ ];
53
+ // Shared hooks definition
54
+ const sharedHooks = {
55
+ afterChange: [
56
+ async ({ doc, req })=>{
57
+ const productIds = doc.items.map((item)=>{
58
+ const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\.html)/);
59
+ return match ? match[0] : null;
60
+ });
61
+ const shopId = req.user?.shops?.[0]?.shop?.id;
62
+ await syncProducts(productIds, req.payload, shopId);
63
+ }
64
+ ]
88
65
  };
66
+ // Global Config
67
+ export const CjSettings = ({ overrides })=>({
68
+ slug: "cj-settings",
69
+ access: {
70
+ ...overrides?.access
71
+ },
72
+ admin: {
73
+ group: "Plugins",
74
+ ...overrides?.admin
75
+ },
76
+ fields: sharedFields,
77
+ hooks: sharedHooks,
78
+ label: "CJ Dropshipping",
79
+ ...overrides || {}
80
+ });
81
+ export const CjConfigCollection = ({ overrides })=>({
82
+ slug: "cj-settings",
83
+ access: {
84
+ ...overrides?.access
85
+ },
86
+ admin: {
87
+ group: "Plugins",
88
+ ...overrides?.admin
89
+ },
90
+ fields: sharedFields,
91
+ hooks: sharedHooks,
92
+ labels: {
93
+ singular: "CJ Dropshipping",
94
+ plural: "CJ Configs"
95
+ },
96
+ ...overrides || {}
97
+ });
89
98
 
90
99
  //# sourceMappingURL=cj-settings.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cj-settings.ts"],"sourcesContent":["import type { GlobalConfig } from \"payload\";\n\nimport { syncProducts } from \"./service/sync-products\";\nimport { decryptToken, encryptToken } from \"./util/manage-tokens\";\n\nexport const CjSettings: GlobalConfig = {\n slug: \"cj-settings\",\n access: {\n read: () => true,\n // update: ({ req }) => !!req.user?.roles?.includes(\"user\"),\n },\n admin: {\n group: \"Plugins\",\n },\n fields: [\n {\n label: \"Credentials\",\n type: \"collapsible\",\n fields: [\n {\n type: \"row\",\n fields: [\n {\n name: \"emailAddress\",\n type: \"text\",\n },\n {\n name: \"apiToken\",\n type: \"text\",\n admin: {\n components: {\n Field: \"@shopnex/cj-plugin/rsc#ApiToken\",\n },\n },\n },\n ],\n },\n ],\n },\n {\n label: \"Logo Area POD\",\n name: \"pod\",\n type: \"upload\",\n relationTo: \"media\",\n },\n {\n name: \"items\",\n type: \"array\",\n admin: {\n description: \"A list of product URLs to sync with CJ Dropshipping\",\n },\n fields: [\n {\n name: \"productUrl\",\n type: \"text\",\n },\n ],\n label: \"Products\",\n labels: {\n plural: \"Product URLs\",\n singular: \"Product URL\",\n },\n },\n ],\n hooks: {\n beforeChange: [\n ({ data }) => {\n const tokenToEncrypt = data.apiToken;\n const encryptedToken = encryptToken(tokenToEncrypt);\n data.apiToken = encryptedToken;\n },\n ],\n beforeRead: [\n ({ doc }) => {\n doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);\n },\n ],\n afterChange: [\n async ({ doc, req: { payload } }) => {\n const productIds = doc.items.map((item: any) => {\n const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\\.html)/);\n return match ? match[0] : null;\n });\n await syncProducts(productIds, payload);\n },\n ({ doc }) => {\n doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);\n },\n ],\n },\n label: \"CJ Dropshipping\",\n};\n"],"names":["syncProducts","decryptToken","encryptToken","CjSettings","slug","access","read","admin","group","fields","label","type","name","components","Field","relationTo","description","labels","plural","singular","hooks","beforeChange","data","tokenToEncrypt","apiToken","encryptedToken","beforeRead","doc","afterChange","req","payload","productIds","items","map","item","match","productUrl"],"mappings":"AAEA,SAASA,YAAY,QAAQ,0BAA0B;AACvD,SAASC,YAAY,EAAEC,YAAY,QAAQ,uBAAuB;AAElE,OAAO,MAAMC,aAA2B;IACpCC,MAAM;IACNC,QAAQ;QACJC,MAAM,IAAM;IAEhB;IACAC,OAAO;QACHC,OAAO;IACX;IACAC,QAAQ;QACJ;YACIC,OAAO;YACPC,MAAM;YACNF,QAAQ;gBACJ;oBACIE,MAAM;oBACNF,QAAQ;wBACJ;4BACIG,MAAM;4BACND,MAAM;wBACV;wBACA;4BACIC,MAAM;4BACND,MAAM;4BACNJ,OAAO;gCACHM,YAAY;oCACRC,OAAO;gCACX;4BACJ;wBACJ;qBACH;gBACL;aACH;QACL;QACA;YACIJ,OAAO;YACPE,MAAM;YACND,MAAM;YACNI,YAAY;QAChB;QACA;YACIH,MAAM;YACND,MAAM;YACNJ,OAAO;gBACHS,aAAa;YACjB;YACAP,QAAQ;gBACJ;oBACIG,MAAM;oBACND,MAAM;gBACV;aACH;YACDD,OAAO;YACPO,QAAQ;gBACJC,QAAQ;gBACRC,UAAU;YACd;QACJ;KACH;IACDC,OAAO;QACHC,cAAc;YACV,CAAC,EAAEC,IAAI,EAAE;gBACL,MAAMC,iBAAiBD,KAAKE,QAAQ;gBACpC,MAAMC,iBAAiBvB,aAAaqB;gBACpCD,KAAKE,QAAQ,GAAGC;YACpB;SACH;QACDC,YAAY;YACR,CAAC,EAAEC,GAAG,EAAE;gBACJA,IAAIH,QAAQ,GAAGG,IAAIH,QAAQ,IAAIvB,aAAa0B,IAAIH,QAAQ;YAC5D;SACH;QACDI,aAAa;YACT,OAAO,EAAED,GAAG,EAAEE,KAAK,EAAEC,OAAO,EAAE,EAAE;gBAC5B,MAAMC,aAAaJ,IAAIK,KAAK,CAACC,GAAG,CAAC,CAACC;oBAC9B,MAAMC,QAAQD,KAAKE,UAAU,CAACD,KAAK,CAAC;oBACpC,OAAOA,QAAQA,KAAK,CAAC,EAAE,GAAG;gBAC9B;gBACA,MAAMnC,aAAa+B,YAAYD;YACnC;YACA,CAAC,EAAEH,GAAG,EAAE;gBACJA,IAAIH,QAAQ,GAAGG,IAAIH,QAAQ,IAAIvB,aAAa0B,IAAIH,QAAQ;YAC5D;SACH;IACL;IACAd,OAAO;AACX,EAAE"}
1
+ {"version":3,"sources":["../src/cj-settings.ts"],"sourcesContent":["import type { CollectionConfig, Field, GlobalConfig } from \"payload\";\nimport { syncProducts } from \"./service/sync-products\";\n\n// Collection Config\nexport type CjCollectionProps = {\n overrides?: Partial<CollectionConfig>;\n};\n\nexport type CjGlobalProps = {\n overrides?: Partial<GlobalConfig>;\n};\n\n// Shared fields definition\nconst sharedFields: Field[] = [\n // {\n // label: \"Credentials\",\n // type: \"collapsible\",\n // fields: [\n // {\n // type: \"row\",\n // fields: [\n // {\n // name: \"emailAddress\",\n // type: \"text\",\n // },\n // {\n // name: \"apiToken\",\n // type: \"text\",\n // admin: {\n // components: {\n // Field: \"@shopnex/cj-plugin/rsc#ApiToken\",\n // },\n // },\n // },\n // ],\n // },\n // ],\n // },\n // {\n // label: \"Logo Area POD\",\n // name: \"pod\",\n // type: \"upload\",\n // relationTo: \"media\",\n // },\n {\n name: \"items\",\n type: \"array\",\n admin: {\n description: \"A list of product URLs to sync with CJ Dropshipping\",\n },\n fields: [\n {\n name: \"productUrl\",\n type: \"text\",\n },\n ],\n label: \"Products\",\n labels: {\n plural: \"Product URLs\",\n singular: \"Product URL\",\n },\n },\n];\n\n// Shared hooks definition\nconst sharedHooks = {\n afterChange: [\n async ({ doc, req }) => {\n const productIds = doc.items.map((item: any) => {\n const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\\.html)/);\n return match ? match[0] : null;\n });\n const shopId = req.user?.shops?.[0]?.shop?.id;\n await syncProducts(productIds, req.payload, shopId);\n },\n ],\n};\n\n// Global Config\nexport const CjSettings = ({ overrides }: CjGlobalProps): GlobalConfig => ({\n slug: \"cj-settings\",\n access: {\n ...overrides?.access,\n },\n admin: {\n group: \"Plugins\",\n ...overrides?.admin,\n },\n fields: sharedFields,\n hooks: sharedHooks,\n label: \"CJ Dropshipping\",\n ...(overrides || {}),\n});\n\nexport const CjConfigCollection = ({ overrides }: CjCollectionProps): CollectionConfig => ({\n slug: \"cj-settings\",\n access: {\n ...overrides?.access,\n },\n admin: {\n group: \"Plugins\",\n ...overrides?.admin,\n },\n fields: sharedFields,\n hooks: sharedHooks,\n labels: {\n singular: \"CJ Dropshipping\",\n plural: \"CJ Configs\",\n },\n ...(overrides || {}),\n});\n"],"names":["syncProducts","sharedFields","name","type","admin","description","fields","label","labels","plural","singular","sharedHooks","afterChange","doc","req","productIds","items","map","item","match","productUrl","shopId","user","shops","shop","id","payload","CjSettings","overrides","slug","access","group","hooks","CjConfigCollection"],"mappings":"AACA,SAASA,YAAY,QAAQ,0BAA0B;AAWvD,2BAA2B;AAC3B,MAAMC,eAAwB;IAC1B,IAAI;IACJ,4BA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AAC,CAAC;IACvB,CAAA,EAAG;AAEH,OAAO,MAAMK,qBAAqB,CAAC,EAAEL,SAAS,EAAqB,GAAwB,CAAA;QACvFC,MAAM;QACNC,QAAQ;YACJ,GAAGF,WAAWE,MAAM;QACxB;QACA1B,OAAO;YACH2B,OAAO;YACP,GAAGH,WAAWxB,KAAK;QACvB;QACAE,QAAQL;QACR+B,OAAOrB;QACPH,QAAQ;YACJE,UAAU;YACVD,QAAQ;QACZ;QACA,GAAImB,aAAa,CAAC,CAAC;IACvB,CAAA,EAAG"}
package/dist/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  import type { Config } from "payload";
2
- import { CjCollectionProps } from "./CjConfig";
2
+ import { CjCollectionProps, CjGlobalProps } from "./cj-settings";
3
3
  interface PluginOptions {
4
+ isEnabled?: boolean;
4
5
  cjApiKey: string;
5
6
  cjEmailAddress: string;
6
7
  cjRefreshToken?: string;
7
- asCollection?: boolean;
8
+ isGlobal?: boolean;
8
9
  collectionOverrides?: CjCollectionProps["overrides"];
10
+ globalOverrides?: CjGlobalProps["overrides"];
11
+ orderCollectionSlug?: string;
9
12
  }
10
13
  export declare const cjPlugin: (pluginOptions: PluginOptions) => (config: Config) => Config;
11
14
  export {};
package/dist/index.js CHANGED
@@ -1,56 +1,67 @@
1
1
  import { setTenantCredentials } from "./sdk/access-token";
2
2
  import { createOrderHook } from "./service/create-order.hook";
3
- import { CjConfigCollection } from "./CjConfig";
4
- const updateCollection = (collection)=>{
5
- if (collection.slug === "orders") {
6
- return {
7
- ...collection,
8
- hooks: {
9
- ...collection.hooks,
10
- afterChange: [
11
- ...collection.hooks?.afterChange || [],
12
- createOrderHook
13
- ]
14
- }
15
- };
16
- }
17
- return collection;
18
- };
3
+ import { CjConfigCollection, CjSettings } from "./cj-settings";
19
4
  export const cjPlugin = (pluginOptions)=>(config)=>{
20
- const updatedCollections = config.collections?.map(updateCollection) || [];
21
- if (pluginOptions.asCollection) {
22
- updatedCollections.push(CjConfigCollection({
5
+ const isGlobal = pluginOptions.isGlobal ?? true;
6
+ const isEnabled = pluginOptions.isEnabled ?? true;
7
+ const ordersCollection = config.collections?.find((collection)=>collection.slug === (pluginOptions.orderCollectionSlug || "orders"));
8
+ if (!ordersCollection) {
9
+ throw new Error("No orders collection found");
10
+ }
11
+ if (!ordersCollection.hooks) {
12
+ ordersCollection.hooks = {};
13
+ }
14
+ if (!ordersCollection.hooks?.afterChange?.length) {
15
+ ordersCollection.hooks.afterChange = [];
16
+ }
17
+ if (isGlobal) {
18
+ config.globals?.push(CjSettings({
19
+ overrides: pluginOptions.globalOverrides
20
+ }));
21
+ } else {
22
+ config.collections?.push(CjConfigCollection({
23
23
  overrides: pluginOptions.collectionOverrides
24
24
  }));
25
25
  }
26
- const productCollection = updatedCollections.find((collection)=>collection.slug === "products");
26
+ const productCollection = config.collections?.find((collection)=>collection.slug === "products");
27
27
  const sourceField = productCollection?.fields?.find((field)=>field.name === "source");
28
28
  sourceField.options.push({
29
29
  label: "CJ",
30
30
  value: "cj"
31
31
  });
32
+ if (!isEnabled) {
33
+ return config;
34
+ }
35
+ if (ordersCollection.hooks?.afterChange) {
36
+ ordersCollection.hooks.afterChange.push(createOrderHook);
37
+ }
32
38
  const incomingOnInit = config.onInit;
33
39
  config.onInit = async (payload)=>{
34
40
  if (incomingOnInit) {
35
41
  await incomingOnInit(payload);
36
42
  }
37
- const cjSettings = await payload.find({
38
- collection: "cj-settings"
39
- });
40
- cjSettings.docs.forEach((config)=>{
41
- setTenantCredentials(config?.shop?.slug || "default", {
43
+ const cjSettingsDocs = [];
44
+ if (isGlobal) {
45
+ setTenantCredentials("1", {
46
+ emailAddress: pluginOptions.cjEmailAddress,
47
+ password: pluginOptions.cjApiKey,
48
+ refreshToken: pluginOptions.cjRefreshToken
49
+ });
50
+ return;
51
+ } else {
52
+ const cjSettings = await payload.find({
53
+ collection: "cj-settings"
54
+ });
55
+ cjSettingsDocs.push(...cjSettings?.docs);
56
+ }
57
+ cjSettingsDocs.forEach((config)=>{
58
+ setTenantCredentials(config?.shop?.slug || "1", {
42
59
  emailAddress: config.email,
43
60
  password: config.apiToken
44
61
  });
45
62
  });
46
63
  };
47
- return {
48
- ...config,
49
- collections: updatedCollections,
50
- globals: [
51
- ...config.globals || []
52
- ]
53
- };
64
+ return config;
54
65
  };
55
66
 
56
67
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CollectionConfig, Config, FieldBase, SelectField } from \"payload\";\nimport { setTenantCredentials } from \"./sdk/access-token\";\nimport { createOrderHook } from \"./service/create-order.hook\";\nimport { CjConfigCollection, CjCollectionProps } from \"./CjConfig\";\n\ninterface PluginOptions {\n cjApiKey: string;\n cjEmailAddress: string;\n cjRefreshToken?: string;\n asCollection?: boolean;\n collectionOverrides?: CjCollectionProps[\"overrides\"];\n}\n\nconst updateCollection = (collection: CollectionConfig) => {\n if (collection.slug === \"orders\") {\n return {\n ...collection,\n hooks: {\n ...collection.hooks,\n afterChange: [...(collection.hooks?.afterChange || []), createOrderHook],\n },\n };\n }\n return collection;\n};\n\nexport const cjPlugin =\n (pluginOptions: PluginOptions) =>\n (config: Config): Config => {\n const updatedCollections = config.collections?.map(updateCollection) || [];\n\n if (pluginOptions.asCollection) {\n updatedCollections.push(\n CjConfigCollection({ overrides: pluginOptions.collectionOverrides }),\n );\n }\n\n const productCollection = updatedCollections.find(\n (collection) => collection.slug === \"products\",\n );\n\n const sourceField = productCollection?.fields?.find(\n (field) => (field as SelectField).name === \"source\",\n ) as SelectField;\n\n sourceField.options.push({\n label: \"CJ\",\n value: \"cj\",\n });\n\n const incomingOnInit = config.onInit;\n\n config.onInit = async (payload) => {\n if (incomingOnInit) {\n await incomingOnInit(payload);\n }\n const cjSettings = await payload.find({\n collection: \"cj-settings\",\n });\n cjSettings.docs.forEach((config: any) => {\n setTenantCredentials(config?.shop?.slug || \"default\", {\n emailAddress: config.email,\n password: config.apiToken,\n });\n });\n };\n\n return {\n ...config,\n collections: updatedCollections,\n globals: [...(config.globals || [])],\n };\n };\n"],"names":["setTenantCredentials","createOrderHook","CjConfigCollection","updateCollection","collection","slug","hooks","afterChange","cjPlugin","pluginOptions","config","updatedCollections","collections","map","asCollection","push","overrides","collectionOverrides","productCollection","find","sourceField","fields","field","name","options","label","value","incomingOnInit","onInit","payload","cjSettings","docs","forEach","shop","emailAddress","email","password","apiToken","globals"],"mappings":"AACA,SAASA,oBAAoB,QAAQ,qBAAqB;AAC1D,SAASC,eAAe,QAAQ,8BAA8B;AAC9D,SAASC,kBAAkB,QAA2B,aAAa;AAUnE,MAAMC,mBAAmB,CAACC;IACtB,IAAIA,WAAWC,IAAI,KAAK,UAAU;QAC9B,OAAO;YACH,GAAGD,UAAU;YACbE,OAAO;gBACH,GAAGF,WAAWE,KAAK;gBACnBC,aAAa;uBAAKH,WAAWE,KAAK,EAAEC,eAAe,EAAE;oBAAGN;iBAAgB;YAC5E;QACJ;IACJ;IACA,OAAOG;AACX;AAEA,OAAO,MAAMI,WACT,CAACC,gBACD,CAACC;QACG,MAAMC,qBAAqBD,OAAOE,WAAW,EAAEC,IAAIV,qBAAqB,EAAE;QAE1E,IAAIM,cAAcK,YAAY,EAAE;YAC5BH,mBAAmBI,IAAI,CACnBb,mBAAmB;gBAAEc,WAAWP,cAAcQ,mBAAmB;YAAC;QAE1E;QAEA,MAAMC,oBAAoBP,mBAAmBQ,IAAI,CAC7C,CAACf,aAAeA,WAAWC,IAAI,KAAK;QAGxC,MAAMe,cAAcF,mBAAmBG,QAAQF,KAC3C,CAACG,QAAU,AAACA,MAAsBC,IAAI,KAAK;QAG/CH,YAAYI,OAAO,CAACT,IAAI,CAAC;YACrBU,OAAO;YACPC,OAAO;QACX;QAEA,MAAMC,iBAAiBjB,OAAOkB,MAAM;QAEpClB,OAAOkB,MAAM,GAAG,OAAOC;YACnB,IAAIF,gBAAgB;gBAChB,MAAMA,eAAeE;YACzB;YACA,MAAMC,aAAa,MAAMD,QAAQV,IAAI,CAAC;gBAClCf,YAAY;YAChB;YACA0B,WAAWC,IAAI,CAACC,OAAO,CAAC,CAACtB;gBACrBV,qBAAqBU,QAAQuB,MAAM5B,QAAQ,WAAW;oBAClD6B,cAAcxB,OAAOyB,KAAK;oBAC1BC,UAAU1B,OAAO2B,QAAQ;gBAC7B;YACJ;QACJ;QAEA,OAAO;YACH,GAAG3B,MAAM;YACTE,aAAaD;YACb2B,SAAS;mBAAK5B,OAAO4B,OAAO,IAAI,EAAE;aAAE;QACxC;IACJ,EAAE"}
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, CjConfigCollection, CjGlobalProps, CjSettings } from \"./cj-settings\";\n\ninterface PluginOptions {\n isEnabled?: boolean;\n cjApiKey: string;\n cjEmailAddress: string;\n cjRefreshToken?: string;\n isGlobal?: boolean;\n collectionOverrides?: CjCollectionProps[\"overrides\"];\n globalOverrides?: CjGlobalProps[\"overrides\"];\n orderCollectionSlug?: string;\n}\n\nexport const cjPlugin =\n (pluginOptions: PluginOptions) =>\n (config: Config): Config => {\n const isGlobal = pluginOptions.isGlobal ?? true;\n\n const isEnabled = pluginOptions.isEnabled ?? true;\n\n const ordersCollection = config.collections?.find(\n (collection) => collection.slug === (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\n if (isGlobal) {\n config.globals?.push(CjSettings({ overrides: pluginOptions.globalOverrides }));\n } else {\n config.collections?.push(\n CjConfigCollection({ overrides: pluginOptions.collectionOverrides }),\n );\n }\n const productCollection = config.collections?.find(\n (collection) => collection.slug === \"products\",\n );\n\n const sourceField = productCollection?.fields?.find(\n (field) => (field as SelectField).name === \"source\",\n ) as SelectField;\n\n sourceField.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 const cjSettingsDocs: any = [];\n\n if (isGlobal) {\n setTenantCredentials(\"1\", {\n emailAddress: pluginOptions.cjEmailAddress,\n password: pluginOptions.cjApiKey,\n refreshToken: pluginOptions.cjRefreshToken,\n });\n return;\n } else {\n const cjSettings = await payload.find({\n collection: \"cj-settings\",\n });\n\n cjSettingsDocs.push(...cjSettings?.docs);\n }\n\n cjSettingsDocs.forEach((config: any) => {\n setTenantCredentials(config?.shop?.slug || \"1\", {\n emailAddress: config.email,\n password: config.apiToken,\n });\n });\n };\n\n return config;\n };\n"],"names":["setTenantCredentials","createOrderHook","CjConfigCollection","CjSettings","cjPlugin","pluginOptions","config","isGlobal","isEnabled","ordersCollection","collections","find","collection","slug","orderCollectionSlug","Error","hooks","afterChange","length","globals","push","overrides","globalOverrides","collectionOverrides","productCollection","sourceField","fields","field","name","options","label","value","incomingOnInit","onInit","payload","cjSettingsDocs","emailAddress","cjEmailAddress","password","cjApiKey","refreshToken","cjRefreshToken","cjSettings","docs","forEach","shop","email","apiToken"],"mappings":"AACA,SAASA,oBAAoB,QAAQ,qBAAqB;AAC1D,SAASC,eAAe,QAAQ,8BAA8B;AAC9D,SAA4BC,kBAAkB,EAAiBC,UAAU,QAAQ,gBAAgB;AAajG,OAAO,MAAMC,WACT,CAACC,gBACD,CAACC;QACG,MAAMC,WAAWF,cAAcE,QAAQ,IAAI;QAE3C,MAAMC,YAAYH,cAAcG,SAAS,IAAI;QAE7C,MAAMC,mBAAmBH,OAAOI,WAAW,EAAEC,KACzC,CAACC,aAAeA,WAAWC,IAAI,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B,IAAI,CAAC;oBAClCC,YAAY;gBAChB;gBAEAuB,eAAef,IAAI,IAAIsB,YAAYC;YACvC;YAEAR,eAAeS,OAAO,CAAC,CAACtC;gBACpBN,qBAAqBM,QAAQuC,MAAMhC,QAAQ,KAAK;oBAC5CuB,cAAc9B,OAAOwC,KAAK;oBAC1BR,UAAUhC,OAAOyC,QAAQ;gBAC7B;YACJ;QACJ;QAEA,OAAOzC;IACX,EAAE"}
@@ -1,7 +1,7 @@
1
1
  import * as cjSdk from "./cj-sdk";
2
2
  const tenantCredentialsMap = new Map();
3
3
  export const getCurrentAccessToken = async ()=>{
4
- const shopId = '1';
4
+ const shopId = "1";
5
5
  const accessToken = await getTenantAccessToken(shopId);
6
6
  return accessToken;
7
7
  };
@@ -14,11 +14,26 @@ export const getTenantAccessToken = async (shopId)=>{
14
14
  throw new Error(`Credentials for tenant ${shopId} are missing or incomplete`);
15
15
  }
16
16
  const { emailAddress, password, refreshToken } = creds;
17
- let accessToken = (await cjSdk.refreshAccessToken(refreshToken || "")).accessToken;
18
- if (!accessToken) {
19
- accessToken = (await cjSdk.getAccessToken(emailAddress, password)).accessToken;
17
+ let newAccessToken;
18
+ let newRefreshToken;
19
+ if (!refreshToken) {
20
+ const result = await cjSdk.getAccessToken(emailAddress, password);
21
+ newAccessToken = result.accessToken;
22
+ newRefreshToken = result.refreshToken;
23
+ tenantCredentialsMap.set(shopId, {
24
+ ...creds,
25
+ refreshToken: newRefreshToken
26
+ });
27
+ } else {
28
+ const result = await cjSdk.refreshAccessToken(refreshToken);
29
+ newAccessToken = result.accessToken;
30
+ newRefreshToken = result.refreshToken;
31
+ tenantCredentialsMap.set(shopId, {
32
+ ...creds,
33
+ refreshToken: newRefreshToken
34
+ });
20
35
  }
21
- return accessToken;
36
+ return newAccessToken;
22
37
  };
23
38
 
24
39
  //# sourceMappingURL=access-token.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/sdk/access-token.ts"],"sourcesContent":["import * as cjSdk from \"./cj-sdk\";\n\ntype Credentials = {\n emailAddress: string;\n password: string;\n refreshToken?: string;\n};\n\nconst tenantCredentialsMap = new Map<string, Credentials>();\n\nexport const getCurrentAccessToken = async () => {\n const shopId = '1'\n const accessToken = await getTenantAccessToken(shopId);\n return accessToken\n};\n\nexport const setTenantCredentials = (shopId: string, creds: Credentials) => {\n tenantCredentialsMap.set(shopId, creds);\n};\n\nexport const getTenantAccessToken = async (shopId: string) => {\n const creds = tenantCredentialsMap.get(shopId);\n\n if (!creds?.emailAddress || !creds?.password) {\n throw new Error(`Credentials for tenant ${shopId} are missing or incomplete`);\n }\n\n const { emailAddress, password, refreshToken } = creds;\n let accessToken = (await cjSdk.refreshAccessToken(refreshToken || \"\")).accessToken;\n if (!accessToken) {\n accessToken = (await cjSdk.getAccessToken(emailAddress, password)).accessToken;\n }\n return accessToken;\n};\n"],"names":["cjSdk","tenantCredentialsMap","Map","getCurrentAccessToken","shopId","accessToken","getTenantAccessToken","setTenantCredentials","creds","set","get","emailAddress","password","Error","refreshToken","refreshAccessToken","getAccessToken"],"mappings":"AAAA,YAAYA,WAAW,WAAW;AAQlC,MAAMC,uBAAuB,IAAIC;AAEjC,OAAO,MAAMC,wBAAwB;IACjC,MAAMC,SAAS;IACf,MAAMC,cAAc,MAAMC,qBAAqBF;IAC/C,OAAOC;AACX,EAAE;AAEF,OAAO,MAAME,uBAAuB,CAACH,QAAgBI;IACjDP,qBAAqBQ,GAAG,CAACL,QAAQI;AACrC,EAAE;AAEF,OAAO,MAAMF,uBAAuB,OAAOF;IACvC,MAAMI,QAAQP,qBAAqBS,GAAG,CAACN;IAEvC,IAAI,CAACI,OAAOG,gBAAgB,CAACH,OAAOI,UAAU;QAC1C,MAAM,IAAIC,MAAM,CAAC,uBAAuB,EAAET,OAAO,0BAA0B,CAAC;IAChF;IAEA,MAAM,EAAEO,YAAY,EAAEC,QAAQ,EAAEE,YAAY,EAAE,GAAGN;IACjD,IAAIH,cAAc,AAAC,CAAA,MAAML,MAAMe,kBAAkB,CAACD,gBAAgB,GAAE,EAAGT,WAAW;IAClF,IAAI,CAACA,aAAa;QACdA,cAAc,AAAC,CAAA,MAAML,MAAMgB,cAAc,CAACL,cAAcC,SAAQ,EAAGP,WAAW;IAClF;IACA,OAAOA;AACX,EAAE"}
1
+ {"version":3,"sources":["../../src/sdk/access-token.ts"],"sourcesContent":["import * as cjSdk from \"./cj-sdk\";\n\ntype Credentials = {\n emailAddress: string;\n password: string;\n refreshToken?: string;\n};\n\nconst tenantCredentialsMap = new Map<string, Credentials>();\n\nexport const getCurrentAccessToken = async () => {\n const shopId = \"1\";\n const accessToken = await getTenantAccessToken(shopId);\n return accessToken;\n};\n\nexport const setTenantCredentials = (shopId: string, creds: Credentials) => {\n tenantCredentialsMap.set(shopId, creds);\n};\n\nexport const getTenantAccessToken = async (shopId: string) => {\n const creds = tenantCredentialsMap.get(shopId);\n\n if (!creds?.emailAddress || !creds?.password) {\n throw new Error(`Credentials for tenant ${shopId} are missing or incomplete`);\n }\n\n const { emailAddress, password, refreshToken } = creds;\n\n let newAccessToken: string;\n let newRefreshToken: string | undefined;\n\n if (!refreshToken) {\n const result = await cjSdk.getAccessToken(emailAddress, password);\n newAccessToken = result.accessToken;\n newRefreshToken = result.refreshToken;\n\n tenantCredentialsMap.set(shopId, {\n ...creds,\n refreshToken: newRefreshToken,\n });\n } else {\n const result = await cjSdk.refreshAccessToken(refreshToken);\n newAccessToken = result.accessToken;\n newRefreshToken = result.refreshToken;\n\n tenantCredentialsMap.set(shopId, {\n ...creds,\n refreshToken: newRefreshToken,\n });\n }\n\n return newAccessToken;\n};\n"],"names":["cjSdk","tenantCredentialsMap","Map","getCurrentAccessToken","shopId","accessToken","getTenantAccessToken","setTenantCredentials","creds","set","get","emailAddress","password","Error","refreshToken","newAccessToken","newRefreshToken","result","getAccessToken","refreshAccessToken"],"mappings":"AAAA,YAAYA,WAAW,WAAW;AAQlC,MAAMC,uBAAuB,IAAIC;AAEjC,OAAO,MAAMC,wBAAwB;IACjC,MAAMC,SAAS;IACf,MAAMC,cAAc,MAAMC,qBAAqBF;IAC/C,OAAOC;AACX,EAAE;AAEF,OAAO,MAAME,uBAAuB,CAACH,QAAgBI;IACjDP,qBAAqBQ,GAAG,CAACL,QAAQI;AACrC,EAAE;AAEF,OAAO,MAAMF,uBAAuB,OAAOF;IACvC,MAAMI,QAAQP,qBAAqBS,GAAG,CAACN;IAEvC,IAAI,CAACI,OAAOG,gBAAgB,CAACH,OAAOI,UAAU;QAC1C,MAAM,IAAIC,MAAM,CAAC,uBAAuB,EAAET,OAAO,0BAA0B,CAAC;IAChF;IAEA,MAAM,EAAEO,YAAY,EAAEC,QAAQ,EAAEE,YAAY,EAAE,GAAGN;IAEjD,IAAIO;IACJ,IAAIC;IAEJ,IAAI,CAACF,cAAc;QACf,MAAMG,SAAS,MAAMjB,MAAMkB,cAAc,CAACP,cAAcC;QACxDG,iBAAiBE,OAAOZ,WAAW;QACnCW,kBAAkBC,OAAOH,YAAY;QAErCb,qBAAqBQ,GAAG,CAACL,QAAQ;YAC7B,GAAGI,KAAK;YACRM,cAAcE;QAClB;IACJ,OAAO;QACH,MAAMC,SAAS,MAAMjB,MAAMmB,kBAAkB,CAACL;QAC9CC,iBAAiBE,OAAOZ,WAAW;QACnCW,kBAAkBC,OAAOH,YAAY;QAErCb,qBAAqBQ,GAAG,CAACL,QAAQ;YAC7B,GAAGI,KAAK;YACRM,cAAcE;QAClB;IACJ;IAEA,OAAOD;AACX,EAAE"}
@@ -1,4 +1,4 @@
1
1
  import type { BasePayload } from "payload";
2
2
  import type { ProductDetails } from "../sdk/products/product-types";
3
3
  export declare const fetchExchangeRates: () => Promise<any>;
4
- export declare const syncProducts: (productIds: string[], payload: BasePayload) => Promise<ProductDetails[]>;
4
+ export declare const syncProducts: (productIds: string[], payload: BasePayload, shopId?: string) => Promise<ProductDetails[]>;
@@ -97,7 +97,7 @@ const findProductById = async (productId)=>{
97
97
  });
98
98
  return result.data;
99
99
  };
100
- const createOrUpdateProduct = async (product, payload)=>{
100
+ const createOrUpdateProduct = async (product, payload, shopId)=>{
101
101
  const { totalDocs } = await payload.count({
102
102
  collection: "products",
103
103
  where: {
@@ -110,7 +110,8 @@ const createOrUpdateProduct = async (product, payload)=>{
110
110
  return payload.create({
111
111
  collection: "products",
112
112
  data: {
113
- ...product
113
+ ...product,
114
+ shop: shopId
114
115
  }
115
116
  });
116
117
  }
@@ -120,7 +121,7 @@ export const fetchExchangeRates = async ()=>{
120
121
  const data = await response.json();
121
122
  return data;
122
123
  };
123
- export const syncProducts = async (productIds, payload)=>{
124
+ export const syncProducts = async (productIds, payload, shopId)=>{
124
125
  const exchangeRates = await fetchExchangeRates();
125
126
  const storeSettings = await payload.findGlobal({
126
127
  slug: "store-settings"
@@ -141,7 +142,7 @@ export const syncProducts = async (productIds, payload)=>{
141
142
  const mappedProducts = products.map((product)=>{
142
143
  return mapMockProductToSchema(product, editorConfig, rate, payload);
143
144
  });
144
- await Promise.all(mappedProducts.map((product)=>createOrUpdateProduct(product, payload)));
145
+ await Promise.all(mappedProducts.map((product)=>createOrUpdateProduct(product, payload, shopId)));
145
146
  return products;
146
147
  };
147
148
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/service/sync-products.ts"],"sourcesContent":["import type { DefaultNodeTypes, TypedEditorState } from \"@payloadcms/richtext-lexical\";\nimport type { BasePayload } from \"payload\";\n\nimport { convertHTMLToLexical, editorConfigFactory } from \"@payloadcms/richtext-lexical\";\nimport decimal from \"decimal.js\";\nimport { JSDOM } from \"jsdom\";\n\nimport type { ProductDetails } from \"../sdk/products/product-types\";\n\nimport * as cjSdk from \"../sdk/cj-sdk\";\nimport path, { dirname, join } from \"path\";\nimport fs from \"fs\";\nimport { promisify } from \"util\";\nimport { pipeline } from \"stream\";\nimport { writeFile } from \"fs/promises\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst streamPipeline = promisify(pipeline);\n\ninterface Product {\n description: TypedEditorState<DefaultNodeTypes>;\n pid: string;\n title: string;\n source: \"manual\" | \"cj\";\n variants?: Array<{\n imageUrl?: string;\n options?: Array<{ option: string; value: string }>;\n price?: number;\n vid: string;\n }>;\n}\n\nconst delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));\n\nconst download = async (uri: string, filename: string): Promise<void> => {\n try {\n const response = await fetch(uri);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch ${uri}: ${response.statusText}`);\n }\n\n const contentType = response.headers.get(\"content-type\");\n const contentLength = response.headers.get(\"content-length\");\n\n console.log(\"content-type:\", contentType);\n console.log(\"content-length:\", contentLength);\n\n const buffer = Buffer.from(await response.arrayBuffer());\n\n await writeFile(filename, buffer);\n\n console.log(\"Download complete:\", filename);\n } catch (error) {\n console.error(\"Error downloading file:\", error);\n }\n};\n\nasync function uploadImageToPayload(src: string, payload: BasePayload): Promise<any | null> {\n const tempFilePath = path.join(__dirname, \"temp-image.jpg\");\n\n try {\n // Fetch the image\n const response = await fetch(src);\n if (!response.ok) throw new Error(`Failed to fetch image: ${response.statusText}`);\n\n // Stream the response body to a temporary file\n await download(src, tempFilePath);\n\n // Upload the image using Payload's Local API\n const uploadedImage = await payload.create({\n collection: \"media\",\n data: {\n alt: \"Some alt text\",\n },\n\n filePath: tempFilePath,\n });\n\n console.log(\"Uploaded media document:\", uploadedImage);\n\n // Clean up: delete the temporary file\n fs.unlink(tempFilePath, (err) => {\n if (err) console.error(\"Error deleting temporary file:\", err);\n });\n\n return {\n id: uploadedImage.id,\n alt: uploadedImage.alt || \"\",\n prefix: \"media\",\n updatedAt: uploadedImage.updatedAt,\n createdAt: uploadedImage.createdAt,\n url: uploadedImage.url,\n thumbnailURL: uploadedImage.thumbnailURL || null,\n filename: uploadedImage.filename,\n mimeType: uploadedImage.mimeType,\n filesize: uploadedImage.filesize,\n width: uploadedImage.width,\n height: uploadedImage.height,\n focalX: 50,\n focalY: 50,\n };\n } catch (error) {\n console.error(\"Error uploading image:\", error);\n }\n}\nfunction mapMockProductToSchema(\n product: ProductDetails,\n editorConfig: any,\n rate: number,\n payload: BasePayload,\n) {\n return {\n description: convertHTMLToLexical({\n editorConfig,\n html: product.description || \"\",\n JSDOM,\n // uploadImage: async (src: string) => {\n // return uploadImageToPayload(src, payload);\n // },\n }),\n source: \"cj\",\n pid: product.pid,\n title: product.productNameEn,\n variants: product.variants?.map((variant) => ({\n imageUrl: variant.variantImage, // Map image URL to 'id' if using media collection\n options: variant.variantKey?.split(\"-\").map((key, index) => ({\n option: index === 0 ? \"Color\" : \"Size\", // Assuming 'Color' and 'Size', adjust keys if needed\n value: key,\n })),\n price: new decimal(variant.variantSellPrice || 0).mul(rate).toNumber().toFixed(2),\n vid: variant.vid,\n })),\n };\n}\n\nconst findProductById = async (productId: string) => {\n const result = await cjSdk.getProductDetails({\n pid: productId,\n });\n return result.data;\n};\n\nconst createOrUpdateProduct = async (\n product: Omit<Product, \"createdAt\" | \"id\" | \"updatedAt\">,\n payload: BasePayload,\n) => {\n const { totalDocs } = await payload.count({\n collection: \"products\",\n where: {\n pid: {\n equals: product.pid,\n },\n },\n });\n\n if (totalDocs === 0) {\n return payload.create({\n collection: \"products\",\n data: { ...product } as any,\n });\n }\n};\n\nexport const fetchExchangeRates = async () => {\n const response = await fetch(\"https://open.er-api.com/v6/latest/USD\");\n const data = await response.json();\n\n return data;\n};\n\nexport const syncProducts = async (productIds: string[], payload: BasePayload) => {\n const exchangeRates = await fetchExchangeRates();\n const storeSettings = await payload.findGlobal({\n slug: \"store-settings\",\n });\n const rate = exchangeRates.rates[storeSettings.currency || \"USD\"];\n\n const editorConfig = await editorConfigFactory.default({\n config: payload.config,\n });\n const products: ProductDetails[] = [];\n for (const productId of productIds) {\n const product = await findProductById(productId);\n if (!product) {\n continue;\n }\n products.push(product);\n await delay(1010);\n }\n const mappedProducts = products.map((product) => {\n return mapMockProductToSchema(product, editorConfig, rate, payload);\n });\n\n await Promise.all(\n mappedProducts.map((product) => createOrUpdateProduct(product as any, payload)),\n );\n\n return products;\n};\n"],"names":["convertHTMLToLexical","editorConfigFactory","decimal","JSDOM","cjSdk","path","dirname","fs","promisify","pipeline","writeFile","fileURLToPath","__filename","url","__dirname","streamPipeline","delay","ms","Promise","resolve","setTimeout","download","uri","filename","response","fetch","ok","Error","statusText","contentType","headers","get","contentLength","console","log","buffer","Buffer","from","arrayBuffer","error","uploadImageToPayload","src","payload","tempFilePath","join","uploadedImage","create","collection","data","alt","filePath","unlink","err","id","prefix","updatedAt","createdAt","thumbnailURL","mimeType","filesize","width","height","focalX","focalY","mapMockProductToSchema","product","editorConfig","rate","description","html","source","pid","title","productNameEn","variants","map","variant","imageUrl","variantImage","options","variantKey","split","key","index","option","value","price","variantSellPrice","mul","toNumber","toFixed","vid","findProductById","productId","result","getProductDetails","createOrUpdateProduct","totalDocs","count","where","equals","fetchExchangeRates","json","syncProducts","productIds","exchangeRates","storeSettings","findGlobal","slug","rates","currency","default","config","products","push","mappedProducts","all"],"mappings":"AAGA,SAASA,oBAAoB,EAAEC,mBAAmB,QAAQ,+BAA+B;AACzF,OAAOC,aAAa,aAAa;AACjC,SAASC,KAAK,QAAQ,QAAQ;AAI9B,YAAYC,WAAW,gBAAgB;AACvC,OAAOC,QAAQC,OAAO,QAAc,OAAO;AAC3C,OAAOC,QAAQ,KAAK;AACpB,SAASC,SAAS,QAAQ,OAAO;AACjC,SAASC,QAAQ,QAAQ,SAAS;AAClC,SAASC,SAAS,QAAQ,cAAc;AACxC,SAASC,aAAa,QAAQ,MAAM;AAEpC,MAAMC,aAAaD,cAAc,YAAYE,GAAG;AAChD,MAAMC,YAAYR,QAAQM;AAE1B,MAAMG,iBAAiBP,UAAUC;AAejC,MAAMO,QAAQ,CAACC,KAAe,IAAIC,QAAQ,CAACC,UAAYC,WAAWD,SAASF;AAE3E,MAAMI,WAAW,OAAOC,KAAaC;IACjC,IAAI;QACA,MAAMC,WAAW,MAAMC,MAAMH;QAE7B,IAAI,CAACE,SAASE,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vB;QACJ;IACJ;IAEA,IAAI0B,cAAc,GAAG;QACjB,OAAOvD,QAAQI,MAAM,CAAC;YAClBC,YAAY;YACZC,MAAM;gBAAE,GAAGiB,OAAO;YAAC;QACvB;IACJ;AACJ;AAEA,OAAO,MAAMoC,qBAAqB;IAC9B,MAAM7E,WAAW,MAAMC,MAAM;IAC7B,MAAMuB,OAAO,MAAMxB,SAAS8E,IAAI;IAEhC,OAAOtD;AACX,EAAE;AAEF,OAAO,MAAMuD,eAAe,OAAOC,YAAsB9D;IACrD,MAAM+D,gBAAgB,MAAMJ;IAC5B,MAAMK,gBAAgB,MAAMhE,QAAQiE,UAAU,CAAC;QAC3CC,MAAM;IACV;IACA,MAAMzC,OAAOsC,cAAcI,KAAK,CAACH,cAAcI,QAAQ,IAAI,MAAM;IAEjE,MAAM5C,eAAe,MAAMjE,oBAAoB8G,OAAO,CAAC;QACnDC,QAAQtE,QAAQsE,MAAM;IAC1B;IACA,MAAMC,WAA6B,EAAE;IACrC,KAAK,MAAMpB,aAAaW,WAAY;QAChC,MAAMvC,UAAU,MAAM2B,gBAAgBC;QACtC,IAAI,CAAC5B,SAAS;YACV;QACJ;QACAgD,SAASC,IAAI,CAACjD;QACd,MAAMjD,MAAM;IAChB;IACA,MAAMmG,iBAAiBF,SAAStC,GAAG,CAAC,CAACV;QACjC,OAAOD,uBAAuBC,SAASC,cAAcC,MAAMzB;IAC/D;IAEA,MAAMxB,QAAQkG,GAAG,CACbD,eAAexC,GAAG,CAAC,CAACV,UAAY+B,sBAAsB/B,SAAgBvB;IAG1E,OAAOuE;AACX,EAAE"}
1
+ {"version":3,"sources":["../../src/service/sync-products.ts"],"sourcesContent":["import type { DefaultNodeTypes, TypedEditorState } from \"@payloadcms/richtext-lexical\";\nimport type { BasePayload } from \"payload\";\n\nimport { convertHTMLToLexical, editorConfigFactory } from \"@payloadcms/richtext-lexical\";\nimport decimal from \"decimal.js\";\nimport { JSDOM } from \"jsdom\";\n\nimport type { ProductDetails } from \"../sdk/products/product-types\";\n\nimport * as cjSdk from \"../sdk/cj-sdk\";\nimport path, { dirname, join } from \"path\";\nimport fs from \"fs\";\nimport { promisify } from \"util\";\nimport { pipeline } from \"stream\";\nimport { writeFile } from \"fs/promises\";\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nconst streamPipeline = promisify(pipeline);\n\ninterface Product {\n description: TypedEditorState<DefaultNodeTypes>;\n pid: string;\n title: string;\n source: \"manual\" | \"cj\";\n variants?: Array<{\n imageUrl?: string;\n options?: Array<{ option: string; value: string }>;\n price?: number;\n vid: string;\n }>;\n}\n\nconst delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));\n\nconst download = async (uri: string, filename: string): Promise<void> => {\n try {\n const response = await fetch(uri);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch ${uri}: ${response.statusText}`);\n }\n\n const contentType = response.headers.get(\"content-type\");\n const contentLength = response.headers.get(\"content-length\");\n\n console.log(\"content-type:\", contentType);\n console.log(\"content-length:\", contentLength);\n\n const buffer = Buffer.from(await response.arrayBuffer());\n\n await writeFile(filename, buffer);\n\n console.log(\"Download complete:\", filename);\n } catch (error) {\n console.error(\"Error downloading file:\", error);\n }\n};\n\nasync function uploadImageToPayload(src: string, payload: BasePayload): Promise<any | null> {\n const tempFilePath = path.join(__dirname, \"temp-image.jpg\");\n\n try {\n // Fetch the image\n const response = await fetch(src);\n if (!response.ok) throw new Error(`Failed to fetch image: ${response.statusText}`);\n\n // Stream the response body to a temporary file\n await download(src, tempFilePath);\n\n // Upload the image using Payload's Local API\n const uploadedImage = await payload.create({\n collection: \"media\",\n data: {\n alt: \"Some alt text\",\n },\n\n filePath: tempFilePath,\n });\n\n console.log(\"Uploaded media document:\", uploadedImage);\n\n // Clean up: delete the temporary file\n fs.unlink(tempFilePath, (err) => {\n if (err) console.error(\"Error deleting temporary file:\", err);\n });\n\n return {\n id: uploadedImage.id,\n alt: uploadedImage.alt || \"\",\n prefix: \"media\",\n updatedAt: uploadedImage.updatedAt,\n createdAt: uploadedImage.createdAt,\n url: uploadedImage.url,\n thumbnailURL: uploadedImage.thumbnailURL || null,\n filename: uploadedImage.filename,\n mimeType: uploadedImage.mimeType,\n filesize: uploadedImage.filesize,\n width: uploadedImage.width,\n height: uploadedImage.height,\n focalX: 50,\n focalY: 50,\n };\n } catch (error) {\n console.error(\"Error uploading image:\", error);\n }\n}\nfunction mapMockProductToSchema(\n product: ProductDetails,\n editorConfig: any,\n rate: number,\n payload: BasePayload,\n) {\n return {\n description: convertHTMLToLexical({\n editorConfig,\n html: product.description || \"\",\n JSDOM,\n // uploadImage: async (src: string) => {\n // return uploadImageToPayload(src, payload);\n // },\n }),\n source: \"cj\",\n pid: product.pid,\n title: product.productNameEn,\n variants: product.variants?.map((variant) => ({\n imageUrl: variant.variantImage, // Map image URL to 'id' if using media collection\n options: variant.variantKey?.split(\"-\").map((key, index) => ({\n option: index === 0 ? \"Color\" : \"Size\", // Assuming 'Color' and 'Size', adjust keys if needed\n value: key,\n })),\n price: new decimal(variant.variantSellPrice || 0).mul(rate).toNumber().toFixed(2),\n vid: variant.vid,\n })),\n };\n}\n\nconst findProductById = async (productId: string) => {\n const result = await cjSdk.getProductDetails({\n pid: productId,\n });\n return result.data;\n};\n\nconst createOrUpdateProduct = async (\n product: Omit<Product, \"createdAt\" | \"id\" | \"updatedAt\">,\n payload: BasePayload,\n shopId?: string,\n) => {\n const { totalDocs } = await payload.count({\n collection: \"products\",\n where: {\n pid: {\n equals: product.pid,\n },\n },\n });\n\n if (totalDocs === 0) {\n return payload.create({\n collection: \"products\",\n data: {\n ...product,\n shop: shopId,\n } as any,\n });\n }\n};\n\nexport const fetchExchangeRates = async () => {\n const response = await fetch(\"https://open.er-api.com/v6/latest/USD\");\n const data = await response.json();\n\n return data;\n};\n\nexport const syncProducts = async (productIds: string[], payload: BasePayload, shopId?: string) => {\n const exchangeRates = await fetchExchangeRates();\n const storeSettings = await payload.findGlobal({\n slug: \"store-settings\",\n });\n const rate = exchangeRates.rates[storeSettings.currency || \"USD\"];\n\n const editorConfig = await editorConfigFactory.default({\n config: payload.config,\n });\n const products: ProductDetails[] = [];\n for (const productId of productIds) {\n const product = await findProductById(productId);\n if (!product) {\n continue;\n }\n products.push(product);\n await delay(1010);\n }\n const mappedProducts = products.map((product) => {\n return mapMockProductToSchema(product, editorConfig, rate, payload);\n });\n\n await Promise.all(\n mappedProducts.map((product) => createOrUpdateProduct(product as any, payload, shopId)),\n );\n\n return products;\n};\n"],"names":["convertHTMLToLexical","editorConfigFactory","decimal","JSDOM","cjSdk","path","dirname","fs","promisify","pipeline","writeFile","fileURLToPath","__filename","url","__dirname","streamPipeline","delay","ms","Promise","resolve","setTimeout","download","uri","filename","response","fetch","ok","Error","statusText","contentType","headers","get","contentLength","console","log","buffer","Buffer","from","arrayBuffer","error","uploadImageToPayload","src","payload","tempFilePath","join","uploadedImage","create","collection","data","alt","filePath","unlink","err","id","prefix","updatedAt","createdAt","thumbnailURL","mimeType","filesize","width","height","focalX","focalY","mapMockProductToSchema","product","editorConfig","rate","description","html","source","pid","title","productNameEn","variants","map","variant","imageUrl","variantImage","options","variantKey","split","key","index","option","value","price","variantSellPrice","mul","toNumber","toFixed","vid","findProductById","productId","result","getProductDetails","createOrUpdateProduct","shopId","totalDocs","count","where","equals","shop","fetchExchangeRates","json","syncProducts","productIds","exchangeRates","storeSettings","findGlobal","slug","rates","currency","default","config","products","push","mappedProducts","all"],"mappings":"AAGA,SAASA,oBAAoB,EAAEC,mBAAmB,QAAQ,+BAA+B;AACzF,OAAOC,aAAa,aAAa;AACjC,SAASC,KAAK,QAAQ,QAAQ;AAI9B,YAAYC,WAAW,gBAAgB;AACvC,OAAOC,QAAQC,OAAO,QAAc,OAAO;AAC3C,OAAOC,QAAQ,KAAK;AACpB,SAASC,SAAS,QAAQ,OAAO;AACjC,SAASC,QAAQ,QAAQ,SAAS;AAClC,SAASC,SAAS,QAAQ,cAAc;AACxC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bD,eAAe1C,GAAG,CAAC,CAACV,UAAY+B,sBAAsB/B,SAAgBvB,SAASuD;IAGnF,OAAOkB;AACX,EAAE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopnex/cj-plugin",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "A blank template to get started with Payload 3.0",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,7 +0,0 @@
1
- import { CollectionConfig } from "payload";
2
- export type CjCollectionProps = {
3
- overrides?: {
4
- access: CollectionConfig["access"];
5
- };
6
- };
7
- export declare const CjConfigCollection: ({ overrides }: CjCollectionProps) => CollectionConfig;
package/dist/CjConfig.js DELETED
@@ -1,93 +0,0 @@
1
- import { decryptToken, encryptToken } from "./util/manage-tokens";
2
- import { syncProducts } from "./service/sync-products";
3
- export const CjConfigCollection = ({ overrides })=>({
4
- slug: "cj-settings",
5
- access: {
6
- ...overrides?.access
7
- },
8
- admin: {
9
- group: "Plugins"
10
- },
11
- fields: [
12
- {
13
- label: "Credentials",
14
- type: "collapsible",
15
- fields: [
16
- {
17
- type: "row",
18
- fields: [
19
- {
20
- name: "emailAddress",
21
- type: "text"
22
- },
23
- {
24
- name: "apiToken",
25
- type: "text",
26
- admin: {
27
- components: {
28
- Field: "@shopnex/cj-plugin/rsc#ApiToken"
29
- }
30
- }
31
- }
32
- ]
33
- }
34
- ]
35
- },
36
- {
37
- label: "Logo Area POD",
38
- name: "pod",
39
- type: "upload",
40
- relationTo: "media"
41
- },
42
- {
43
- name: "items",
44
- type: "array",
45
- admin: {
46
- description: "A list of product URLs to sync with CJ Dropshipping"
47
- },
48
- fields: [
49
- {
50
- name: "productUrl",
51
- type: "text"
52
- }
53
- ],
54
- label: "Products",
55
- labels: {
56
- plural: "Product URLs",
57
- singular: "Product URL"
58
- }
59
- }
60
- ],
61
- hooks: {
62
- beforeChange: [
63
- ({ data })=>{
64
- const tokenToEncrypt = data.apiToken;
65
- const encryptedToken = encryptToken(tokenToEncrypt);
66
- data.apiToken = encryptedToken;
67
- }
68
- ],
69
- beforeRead: [
70
- ({ doc })=>{
71
- doc.apiToken = doc?.apiToken && decryptToken(doc.apiToken);
72
- }
73
- ],
74
- afterChange: [
75
- async ({ doc, req: { payload } })=>{
76
- const productIds = doc.items.map((item)=>{
77
- const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\.html)/);
78
- return match ? match[0] : null;
79
- });
80
- await syncProducts(productIds, payload);
81
- },
82
- ({ doc })=>{
83
- doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);
84
- }
85
- ]
86
- },
87
- labels: {
88
- singular: "CJ Dropshipping",
89
- plural: "CJ Configs"
90
- }
91
- });
92
-
93
- //# sourceMappingURL=CjConfig.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/CjConfig.ts"],"sourcesContent":["import { CollectionConfig } from \"payload\";\nimport { decryptToken, encryptToken } from \"./util/manage-tokens\";\nimport { syncProducts } from \"./service/sync-products\";\n\nexport type CjCollectionProps = {\n overrides?: {\n access: CollectionConfig[\"access\"];\n };\n};\n\nexport const CjConfigCollection = ({ overrides }: CjCollectionProps): CollectionConfig => ({\n slug: \"cj-settings\",\n access: {\n ...overrides?.access,\n },\n admin: {\n group: \"Plugins\",\n },\n fields: [\n {\n label: \"Credentials\",\n type: \"collapsible\",\n fields: [\n {\n type: \"row\",\n fields: [\n {\n name: \"emailAddress\",\n type: \"text\",\n },\n {\n name: \"apiToken\",\n type: \"text\",\n admin: {\n components: {\n Field: \"@shopnex/cj-plugin/rsc#ApiToken\",\n },\n },\n },\n ],\n },\n ],\n },\n {\n label: \"Logo Area POD\",\n name: \"pod\",\n type: \"upload\",\n relationTo: \"media\",\n },\n {\n name: \"items\",\n type: \"array\",\n admin: {\n description: \"A list of product URLs to sync with CJ Dropshipping\",\n },\n fields: [\n {\n name: \"productUrl\",\n type: \"text\",\n },\n ],\n label: \"Products\",\n labels: {\n plural: \"Product URLs\",\n singular: \"Product URL\",\n },\n },\n ],\n hooks: {\n beforeChange: [\n ({ data }) => {\n const tokenToEncrypt = data.apiToken;\n const encryptedToken = encryptToken(tokenToEncrypt);\n data.apiToken = encryptedToken;\n },\n ],\n beforeRead: [\n ({ doc }) => {\n doc.apiToken = doc?.apiToken && decryptToken(doc.apiToken);\n },\n ],\n afterChange: [\n async ({ doc, req: { payload } }) => {\n const productIds = doc.items.map((item: any) => {\n const match = item.productUrl.match(/(?<=-p-)([0-9A-Fa-f-]+)(?=\\.html)/);\n return match ? match[0] : null;\n });\n await syncProducts(productIds, payload);\n },\n ({ doc }) => {\n doc.apiToken = doc.apiToken && decryptToken(doc.apiToken);\n },\n ],\n },\n labels: {\n singular: \"CJ Dropshipping\",\n plural: \"CJ Configs\",\n },\n});\n"],"names":["decryptToken","encryptToken","syncProducts","CjConfigCollection","overrides","slug","access","admin","group","fields","label","type","name","components","Field","relationTo","description","labels","plural","singular","hooks","beforeChange","data","tokenToEncrypt","apiToken","encryptedToken","beforeRead","doc","afterChange","req","payload","productIds","items","map","item","match","productUrl"],"mappings":"AACA,SAASA,YAAY,EAAEC,YAAY,QAAQ,uBAAuB;AAClE,SAASC,YAAY,QAAQ,0BAA0B;AAQvD,OAAO,MAAMC,qBAAqB,CAAC,EAAEC,SAAS,EAAqB,GAAwB,CAAA;QACvFC,MAAM;QACNC,QAAQ;YACJ,GAAGF,WAAWE,MAAM;QACxB;QACAC,OAAO;YACHC,OAAO;QACX;QACAC,QAAQ;YACJ;gBACIC,OAAO;gBACPC,MAAM;gBACNF,QAAQ;oBACJ;wBACIE,MAAM;wBACNF,QAAQ;4BACJ;gCACIG,MAAM;gCACND,MAAM;4BACV;4BACA;gCACIC,MAAM;gCACND,MAAM;gCACNJ,OAAO;oCACHM,YAAY;wCACRC,OAAO;oCACX;gCACJ;4BACJ;yBACH;oBACL;iBACH;YACL;YACA;gBACIJ,OAAO;gBACPE,MAAM;gBACND,MAAM;gBACNI,YAAY;YAChB;YACA;gBACIH,MAAM;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