@unchainedshop/platform 2.0.0-beta7 → 2.0.0-beta9

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.
Files changed (111) hide show
  1. package/jest.config.js +9 -0
  2. package/lib/bulk-importer/handlers/assortment/create.d.ts +3 -4
  3. package/lib/bulk-importer/handlers/assortment/create.js +5 -10
  4. package/lib/bulk-importer/handlers/assortment/create.js.map +1 -1
  5. package/lib/bulk-importer/handlers/assortment/remove.d.ts +2 -2
  6. package/lib/bulk-importer/handlers/assortment/remove.js +2 -2
  7. package/lib/bulk-importer/handlers/assortment/remove.js.map +1 -1
  8. package/lib/bulk-importer/handlers/assortment/update.d.ts +3 -4
  9. package/lib/bulk-importer/handlers/assortment/update.js +4 -8
  10. package/lib/bulk-importer/handlers/assortment/update.js.map +1 -1
  11. package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.d.ts +3 -4
  12. package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js +10 -7
  13. package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js.map +1 -1
  14. package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.d.ts +3 -4
  15. package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js +3 -3
  16. package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js.map +1 -1
  17. package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.d.ts +3 -4
  18. package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.js +3 -4
  19. package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.js.map +1 -1
  20. package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.d.ts +3 -4
  21. package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js +8 -7
  22. package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js.map +1 -1
  23. package/lib/bulk-importer/handlers/assortment/upsertMedia.d.ts +3 -4
  24. package/lib/bulk-importer/handlers/assortment/upsertMedia.js +8 -38
  25. package/lib/bulk-importer/handlers/assortment/upsertMedia.js.map +1 -1
  26. package/lib/bulk-importer/handlers/filter/create.d.ts +3 -4
  27. package/lib/bulk-importer/handlers/filter/create.js +4 -6
  28. package/lib/bulk-importer/handlers/filter/create.js.map +1 -1
  29. package/lib/bulk-importer/handlers/filter/remove.d.ts +2 -2
  30. package/lib/bulk-importer/handlers/filter/remove.js +2 -2
  31. package/lib/bulk-importer/handlers/filter/remove.js.map +1 -1
  32. package/lib/bulk-importer/handlers/filter/update.d.ts +3 -4
  33. package/lib/bulk-importer/handlers/filter/update.js +3 -4
  34. package/lib/bulk-importer/handlers/filter/update.js.map +1 -1
  35. package/lib/bulk-importer/handlers/filter/upsertFilterContent.d.ts +3 -5
  36. package/lib/bulk-importer/handlers/filter/upsertFilterContent.js +3 -3
  37. package/lib/bulk-importer/handlers/filter/upsertFilterContent.js.map +1 -1
  38. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.d.ts +2 -2
  39. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js +2 -2
  40. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js.map +1 -1
  41. package/lib/bulk-importer/handlers/product/create.d.ts +3 -4
  42. package/lib/bulk-importer/handlers/product/create.js +5 -9
  43. package/lib/bulk-importer/handlers/product/create.js.map +1 -1
  44. package/lib/bulk-importer/handlers/product/remove.d.ts +2 -2
  45. package/lib/bulk-importer/handlers/product/remove.js +1 -1
  46. package/lib/bulk-importer/handlers/product/remove.js.map +1 -1
  47. package/lib/bulk-importer/handlers/product/update.d.ts +3 -4
  48. package/lib/bulk-importer/handlers/product/update.js +4 -7
  49. package/lib/bulk-importer/handlers/product/update.js.map +1 -1
  50. package/lib/bulk-importer/handlers/product/upsertMedia.d.ts +3 -4
  51. package/lib/bulk-importer/handlers/product/upsertMedia.js +8 -38
  52. package/lib/bulk-importer/handlers/product/upsertMedia.js.map +1 -1
  53. package/lib/bulk-importer/handlers/product/upsertProductContent.d.ts +3 -4
  54. package/lib/bulk-importer/handlers/product/upsertProductContent.js +3 -3
  55. package/lib/bulk-importer/handlers/product/upsertProductContent.js.map +1 -1
  56. package/lib/bulk-importer/handlers/product/upsertVariations.d.ts +3 -4
  57. package/lib/bulk-importer/handlers/product/upsertVariations.js +16 -13
  58. package/lib/bulk-importer/handlers/product/upsertVariations.js.map +1 -1
  59. package/lib/bulk-importer/upsertAsset.d.ts +7 -0
  60. package/lib/bulk-importer/upsertAsset.js +33 -0
  61. package/lib/bulk-importer/upsertAsset.js.map +1 -0
  62. package/lib/context/setAccessToken.js.map +1 -1
  63. package/lib/context/withAccessToken.d.ts +2 -1
  64. package/lib/context/withAccessToken.js.map +1 -1
  65. package/lib/platform-index.d.ts +4 -0
  66. package/lib/platform-index.js +3 -0
  67. package/lib/platform-index.js.map +1 -1
  68. package/lib/setup/setupAccounts.js +2 -3
  69. package/lib/setup/setupAccounts.js.map +1 -1
  70. package/lib/setup/setupCarts.js.map +1 -1
  71. package/lib/startPlatform.d.ts +6 -2
  72. package/lib/startPlatform.js +3 -5
  73. package/lib/startPlatform.js.map +1 -1
  74. package/lib/templates/resolveQuotationStatusTemplate.js +1 -1
  75. package/lib/templates/resolveQuotationStatusTemplate.js.map +1 -1
  76. package/lib/templates/utils/getOrderAttachmentsData.d.ts +2 -2
  77. package/lib/templates/utils/getOrderAttachmentsData.js +3 -10
  78. package/lib/templates/utils/getOrderAttachmentsData.js.map +1 -1
  79. package/lib/templates/utils/getOrderPositionsData.d.ts +2 -2
  80. package/lib/templates/utils/getOrderPositionsData.js.map +1 -1
  81. package/package.json +15 -14
  82. package/src/bulk-importer/handlers/assortment/create.ts +9 -18
  83. package/src/bulk-importer/handlers/assortment/remove.ts +4 -4
  84. package/src/bulk-importer/handlers/assortment/update.ts +5 -13
  85. package/src/bulk-importer/handlers/assortment/upsertAssortmentChildren.ts +10 -20
  86. package/src/bulk-importer/handlers/assortment/upsertAssortmentContent.ts +5 -12
  87. package/src/bulk-importer/handlers/assortment/upsertAssortmentFilters.ts +4 -5
  88. package/src/bulk-importer/handlers/assortment/upsertAssortmentProducts.ts +11 -20
  89. package/src/bulk-importer/handlers/assortment/upsertMedia.ts +15 -58
  90. package/src/bulk-importer/handlers/filter/create.ts +4 -8
  91. package/src/bulk-importer/handlers/filter/remove.ts +4 -4
  92. package/src/bulk-importer/handlers/filter/update.ts +4 -6
  93. package/src/bulk-importer/handlers/filter/upsertFilterContent.ts +5 -15
  94. package/src/bulk-importer/handlers/filter/upsertFilterOptionContent.ts +12 -12
  95. package/src/bulk-importer/handlers/product/create.ts +12 -23
  96. package/src/bulk-importer/handlers/product/remove.ts +3 -3
  97. package/src/bulk-importer/handlers/product/update.ts +7 -14
  98. package/src/bulk-importer/handlers/product/upsertMedia.ts +14 -54
  99. package/src/bulk-importer/handlers/product/upsertProductContent.ts +5 -15
  100. package/src/bulk-importer/handlers/product/upsertVariations.ts +26 -34
  101. package/src/bulk-importer/upsertAsset.ts +44 -0
  102. package/src/context/setAccessToken.ts +1 -1
  103. package/src/context/withAccessToken.ts +2 -1
  104. package/src/platform-index.ts +6 -0
  105. package/src/setup/setupAccounts.ts +7 -10
  106. package/src/setup/setupCarts.ts +1 -0
  107. package/src/startPlatform.ts +8 -8
  108. package/src/templates/resolveQuotationStatusTemplate.ts +2 -2
  109. package/src/templates/utils/getOrderAttachmentsData.ts +8 -15
  110. package/src/templates/utils/getOrderPositionsData.ts +2 -2
  111. package/tests/platform-index.test.ts +5 -0
@@ -0,0 +1,44 @@
1
+ import { UnchainedCore } from '@unchainedshop/types/core';
2
+ import { File } from '@unchainedshop/types/files';
3
+
4
+ const upsertAsset = async (
5
+ directoryName: string,
6
+ asset: File & { fileName: string; headers?: Record<string, unknown> },
7
+ unchainedAPI: UnchainedCore,
8
+ ) => {
9
+ const { modules, services } = unchainedAPI;
10
+ const { _id: fileId, fileName, url, meta, headers, ...assetData } = asset;
11
+
12
+ try {
13
+ const existingFile = fileId && (await modules.files.findFile({ fileId }));
14
+ if (existingFile) {
15
+ const newMeta = { ...meta, fileId };
16
+ if (JSON.stringify(newMeta) === JSON.stringify(existingFile.meta)) {
17
+ return existingFile;
18
+ }
19
+ await modules.files.update(fileId, { meta: newMeta, ...assetData });
20
+ const updatedFile = await modules.files.findFile({ fileId });
21
+ return updatedFile;
22
+ }
23
+
24
+ const assetObject = await services.files.uploadFileFromURL(
25
+ {
26
+ directoryName,
27
+ fileInput: {
28
+ fileLink: url,
29
+ fileName,
30
+ headers,
31
+ },
32
+ meta: { ...meta, fileId },
33
+ },
34
+ unchainedAPI,
35
+ );
36
+
37
+ if (!assetObject) throw new Error('Media not created');
38
+ return assetObject;
39
+ } catch (e) {
40
+ return null;
41
+ }
42
+ };
43
+
44
+ export default upsertAsset;
@@ -1,6 +1,6 @@
1
1
  import { UnchainedCore } from '@unchainedshop/types/core';
2
2
 
3
- export default async (unchainedAPI: UnchainedCore, username: string, secret: string) => {
3
+ export default async (unchainedAPI: UnchainedCore, username: string, secret: string): Promise<void> => {
4
4
  await unchainedAPI.modules.users.updateUser(
5
5
  { username },
6
6
  {
@@ -1,10 +1,11 @@
1
1
  import { createLogger } from '@unchainedshop/logger';
2
+ import { Context } from '@unchainedshop/types/api';
2
3
 
3
4
  const logger = createLogger('unchained:platform');
4
5
 
5
6
  const contextIdentity = (params) => params;
6
7
 
7
- export default (fn = contextIdentity) => {
8
+ export default (fn: (context: Context) => any = contextIdentity): any => {
8
9
  return async (params, unchainedContextFn) => {
9
10
  const unchainedContext = await unchainedContextFn(params);
10
11
  const newContext = {
@@ -1,2 +1,8 @@
1
+ import { express } from '@unchainedshop/api';
2
+
1
3
  export * from './startPlatform';
2
4
  export * from './context';
5
+
6
+ const connectPlatformToExpress4 = express.connect;
7
+
8
+ export { connectPlatformToExpress4 };
@@ -29,14 +29,11 @@ export const setupAccounts = (unchainedAPI: UnchainedCore) => {
29
29
  if (!input) return true;
30
30
  if (input.skipMessaging) return true;
31
31
 
32
- return unchainedAPI.modules.worker.addWork(
33
- {
34
- type: 'MESSAGE',
35
- retries: 0,
36
- input,
37
- },
38
- undefined,
39
- );
32
+ return unchainedAPI.modules.worker.addWork({
33
+ type: 'MESSAGE',
34
+ retries: 0,
35
+ input,
36
+ });
40
37
  };
41
38
 
42
39
  accountsServer.services.guest = {
@@ -47,10 +44,8 @@ export const setupAccounts = (unchainedAPI: UnchainedCore) => {
47
44
  {
48
45
  email: params.email || `${guestname}@unchained.local`,
49
46
  guest: true,
50
- profile: {},
51
47
  password: null,
52
48
  initialPassword: true,
53
- lastBillingAddress: {},
54
49
  },
55
50
  {},
56
51
  );
@@ -78,6 +73,8 @@ export const setupAccounts = (unchainedAPI: UnchainedCore) => {
78
73
 
79
74
  accountsServer.on('LoginTokenCreated', async (props) => {
80
75
  const { userId, connection = {} } = props;
76
+
77
+ // TODO: Doubt that there is countryContext etc. here?
81
78
  const { userIdBeforeLogin, countryContext, remoteAddress, remotePort, userAgent, normalizedLocale } =
82
79
  connection;
83
80
 
@@ -17,6 +17,7 @@ export const setupCarts = async (unchainedAPI: UnchainedCore, options: SetupCart
17
17
  await Promise.all(
18
18
  users.map((user) => {
19
19
  const locale = unchainedAPI.modules.users.userLocale(user);
20
+
20
21
  return unchainedAPI.modules.orders.ensureCartForUser(
21
22
  { user, countryCode: locale.country },
22
23
  unchainedAPI,
@@ -3,6 +3,7 @@ import { startAPIServer, roles } from '@unchainedshop/api';
3
3
  import { initCore } from '@unchainedshop/core';
4
4
  import { initDb } from '@unchainedshop/mongodb';
5
5
  import { createLogger } from '@unchainedshop/logger';
6
+ import { UnchainedCore } from '@unchainedshop/types/core';
6
7
  import { createBulkImporterFactory } from './bulk-importer/createBulkImporter';
7
8
  import { runMigrations } from './migrations/runMigrations';
8
9
  import { generateEventTypeDefs } from './setup/generateEventTypeDefs';
@@ -35,7 +36,7 @@ const checkWorkQueueEnabled = (options: SetupWorkqueueOptions) => {
35
36
  return !UNCHAINED_DISABLE_WORKER;
36
37
  };
37
38
 
38
- export const queueWorkers = [];
39
+ export const queueWorkers: Array<any> = [];
39
40
 
40
41
  export const startPlatform = async ({
41
42
  modules = {},
@@ -44,7 +45,6 @@ export const startPlatform = async ({
44
45
  resolvers = [],
45
46
  options = {},
46
47
  rolesOptions = {},
47
- expressApp,
48
48
  bulkImporter: bulkImporterOptions,
49
49
  schema,
50
50
  plugins,
@@ -55,8 +55,10 @@ export const startPlatform = async ({
55
55
  playground,
56
56
  tracing,
57
57
  cacheControl,
58
- corsOrigins,
59
- }: PlatformOptions) => {
58
+ }: PlatformOptions): Promise<{
59
+ unchainedAPI: UnchainedCore;
60
+ apolloGraphQLServer: any;
61
+ }> => {
60
62
  exitOnMissingEnvironmentVariables();
61
63
 
62
64
  // Configure database
@@ -97,20 +99,18 @@ export const startPlatform = async ({
97
99
  ];
98
100
 
99
101
  // Start the graphQL server
100
- await startAPIServer({
102
+ const apolloGraphQLServer = await startAPIServer({
101
103
  unchainedAPI,
102
104
  roles: configuredRoles,
103
105
  typeDefs: [...generatedTypeDefs, ...typeDefs],
104
106
  resolvers,
105
107
  schema,
106
108
  plugins,
107
- expressApp,
108
109
  cache,
109
110
  context,
110
111
  introspection,
111
112
  playground,
112
113
  tracing,
113
- corsOrigins,
114
114
  cacheControl,
115
115
  });
116
116
 
@@ -126,5 +126,5 @@ export const startPlatform = async ({
126
126
  setImmediate(() => unchainedAPI.modules.filters.invalidateCache({}, unchainedAPI));
127
127
  }
128
128
 
129
- return unchainedAPI;
129
+ return { unchainedAPI, apolloGraphQLServer };
130
130
  };
@@ -19,8 +19,8 @@ export const resolveQuotationStatusTemplate: TemplateResolver = async (
19
19
  const quotation = await modules.quotations.findQuotation({ quotationId });
20
20
  const user = await modules.users.findUserById(quotation.userId);
21
21
  const attachments = (
22
- await modules.files.findFilesByMetaData(
23
- { meta: { quotationId: quotation._id, type: 'PROPOSAL ' } },
22
+ await modules.files.findFiles(
23
+ { 'meta.quotationId': quotation._id, 'meta.type': 'PROPOSAL' },
24
24
  { sort: { 'meta.date': -1 } },
25
25
  )
26
26
  )
@@ -1,18 +1,15 @@
1
- import { Context } from '@unchainedshop/types/api';
1
+ import { UnchainedCore } from '@unchainedshop/types/core';
2
2
  import { Order } from '@unchainedshop/types/orders';
3
3
 
4
4
  export const getOrderAttachmentsData = async (
5
5
  order: Order,
6
6
  params: { fileType: string },
7
- { modules }: Context,
7
+ { modules }: UnchainedCore,
8
8
  ) => {
9
- const orderId = order._id;
10
9
  const attachments = [];
11
10
 
12
- const orderFiles = await modules.files.findFilesByMetaData(
13
- {
14
- meta: { orderId: order._id, type: params.fileType },
15
- },
11
+ const orderFiles = await modules.files.findFiles(
12
+ { 'meta.orderId': order._id, 'meta.type': params.fileType },
16
13
  { limit: 1 },
17
14
  );
18
15
  attachments.concat(orderFiles);
@@ -23,18 +20,14 @@ export const getOrderAttachmentsData = async (
23
20
  { limit: 1 },
24
21
  );
25
22
  if (modules.orders.payments.isBlockingOrderFullfillment(payment)) {
26
- const invoices = await modules.files.findFilesByMetaData(
27
- {
28
- meta: { orderId, type: 'INVOICE' },
29
- },
23
+ const invoices = await modules.files.findFiles(
24
+ { 'meta.orderId': order._id, 'meta.type': 'INVOICE' },
30
25
  { limit: 1 },
31
26
  );
32
27
  attachments.concat(invoices);
33
28
  } else {
34
- const receipts = await modules.files.findFilesByMetaData(
35
- {
36
- meta: { orderId, type: 'RECEIPT' },
37
- },
29
+ const receipts = await modules.files.findFiles(
30
+ { 'meta.orderId': order._id, 'meta.type': 'RECEIPT' },
38
31
  { limit: 1 },
39
32
  );
40
33
  attachments.concat(receipts);
@@ -1,11 +1,11 @@
1
- import { Context } from '@unchainedshop/types/api';
2
1
  import { Locale } from '@unchainedshop/types/common';
2
+ import { UnchainedCore } from '@unchainedshop/types/core';
3
3
  import { Order } from '@unchainedshop/types/orders';
4
4
 
5
5
  export const getOrderPositionsData = async (
6
6
  order: Order,
7
7
  params: { locale?: Locale },
8
- context: Context,
8
+ context: UnchainedCore,
9
9
  ) => {
10
10
  const { modules } = context;
11
11
  const orderPositions = await modules.orders.positions.findOrderPositions({
@@ -0,0 +1,5 @@
1
+ describe('Platform', () => {
2
+ it('Init', async () => {
3
+ expect(true).toBeTruthy()
4
+ });
5
+ });