@unchainedshop/platform 2.0.0-beta6 → 2.0.0-beta8

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 (84) hide show
  1. package/lib/bulk-importer/createBulkImporter.js +1 -1
  2. package/lib/bulk-importer/createBulkImporter.js.map +1 -1
  3. package/lib/bulk-importer/handlers/assortment/create.js +6 -3
  4. package/lib/bulk-importer/handlers/assortment/create.js.map +1 -1
  5. package/lib/bulk-importer/handlers/assortment/remove.js +2 -2
  6. package/lib/bulk-importer/handlers/assortment/remove.js.map +1 -1
  7. package/lib/bulk-importer/handlers/assortment/update.js +3 -5
  8. package/lib/bulk-importer/handlers/assortment/update.js.map +1 -1
  9. package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js +2 -2
  10. package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js.map +1 -1
  11. package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js +0 -3
  12. package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js.map +1 -1
  13. package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js +2 -2
  14. package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js.map +1 -1
  15. package/lib/bulk-importer/handlers/assortment/upsertMedia.js +2 -31
  16. package/lib/bulk-importer/handlers/assortment/upsertMedia.js.map +1 -1
  17. package/lib/bulk-importer/handlers/filter/create.js +7 -5
  18. package/lib/bulk-importer/handlers/filter/create.js.map +1 -1
  19. package/lib/bulk-importer/handlers/filter/remove.js +2 -2
  20. package/lib/bulk-importer/handlers/filter/remove.js.map +1 -1
  21. package/lib/bulk-importer/handlers/filter/update.js +12 -13
  22. package/lib/bulk-importer/handlers/filter/update.js.map +1 -1
  23. package/lib/bulk-importer/handlers/filter/upsertFilterContent.d.ts +2 -2
  24. package/lib/bulk-importer/handlers/filter/upsertFilterContent.js +2 -2
  25. package/lib/bulk-importer/handlers/filter/upsertFilterContent.js.map +1 -1
  26. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.d.ts +2 -2
  27. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js +2 -2
  28. package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js.map +1 -1
  29. package/lib/bulk-importer/handlers/product/create.js +3 -0
  30. package/lib/bulk-importer/handlers/product/create.js.map +1 -1
  31. package/lib/bulk-importer/handlers/product/remove.js +3 -3
  32. package/lib/bulk-importer/handlers/product/remove.js.map +1 -1
  33. package/lib/bulk-importer/handlers/product/update.js +3 -5
  34. package/lib/bulk-importer/handlers/product/update.js.map +1 -1
  35. package/lib/bulk-importer/handlers/product/upsertMedia.js +2 -31
  36. package/lib/bulk-importer/handlers/product/upsertMedia.js.map +1 -1
  37. package/lib/bulk-importer/handlers/product/upsertProductContent.js +0 -3
  38. package/lib/bulk-importer/handlers/product/upsertProductContent.js.map +1 -1
  39. package/lib/bulk-importer/upsertAsset.d.ts +7 -0
  40. package/lib/bulk-importer/upsertAsset.js +34 -0
  41. package/lib/bulk-importer/upsertAsset.js.map +1 -0
  42. package/lib/context/setAccessToken.d.ts +2 -2
  43. package/lib/context/setAccessToken.js.map +1 -1
  44. package/lib/context/withAccessToken.d.ts +2 -1
  45. package/lib/context/withAccessToken.js.map +1 -1
  46. package/lib/platform-index.d.ts +4 -0
  47. package/lib/platform-index.js +3 -0
  48. package/lib/platform-index.js.map +1 -1
  49. package/lib/setup/setupAccounts.js +0 -2
  50. package/lib/setup/setupAccounts.js.map +1 -1
  51. package/lib/startPlatform.d.ts +6 -2
  52. package/lib/startPlatform.js +3 -5
  53. package/lib/startPlatform.js.map +1 -1
  54. package/lib/templates/resolveQuotationStatusTemplate.js +1 -1
  55. package/lib/templates/resolveQuotationStatusTemplate.js.map +1 -1
  56. package/lib/templates/utils/getOrderAttachmentsData.js +3 -10
  57. package/lib/templates/utils/getOrderAttachmentsData.js.map +1 -1
  58. package/package.json +14 -14
  59. package/src/bulk-importer/createBulkImporter.ts +1 -1
  60. package/src/bulk-importer/handlers/assortment/create.ts +7 -3
  61. package/src/bulk-importer/handlers/assortment/remove.ts +2 -2
  62. package/src/bulk-importer/handlers/assortment/update.ts +4 -7
  63. package/src/bulk-importer/handlers/assortment/upsertAssortmentChildren.ts +1 -2
  64. package/src/bulk-importer/handlers/assortment/upsertAssortmentContent.ts +0 -3
  65. package/src/bulk-importer/handlers/assortment/upsertAssortmentProducts.ts +1 -2
  66. package/src/bulk-importer/handlers/assortment/upsertMedia.ts +2 -38
  67. package/src/bulk-importer/handlers/filter/create.ts +8 -5
  68. package/src/bulk-importer/handlers/filter/remove.ts +2 -2
  69. package/src/bulk-importer/handlers/filter/update.ts +18 -17
  70. package/src/bulk-importer/handlers/filter/upsertFilterContent.ts +5 -2
  71. package/src/bulk-importer/handlers/filter/upsertFilterOptionContent.ts +2 -2
  72. package/src/bulk-importer/handlers/product/create.ts +4 -0
  73. package/src/bulk-importer/handlers/product/remove.ts +3 -3
  74. package/src/bulk-importer/handlers/product/update.ts +4 -6
  75. package/src/bulk-importer/handlers/product/upsertMedia.ts +2 -38
  76. package/src/bulk-importer/handlers/product/upsertProductContent.ts +0 -3
  77. package/src/bulk-importer/upsertAsset.ts +45 -0
  78. package/src/context/setAccessToken.ts +2 -2
  79. package/src/context/withAccessToken.ts +2 -1
  80. package/src/platform-index.ts +6 -0
  81. package/src/setup/setupAccounts.ts +0 -2
  82. package/src/startPlatform.ts +8 -8
  83. package/src/templates/resolveQuotationStatusTemplate.ts +2 -2
  84. package/src/templates/utils/getOrderAttachmentsData.ts +6 -13
@@ -41,6 +41,10 @@ export default async function createProduct(
41
41
  );
42
42
  }
43
43
 
44
+ if (!(await modules.products.productExists({ productId: _id }))) {
45
+ throw new Error(`Can't create product ${_id}, fields missing?`);
46
+ }
47
+
44
48
  logger.debug('create localized content for product', specification.content);
45
49
  await upsertProductContent(
46
50
  {
@@ -1,11 +1,11 @@
1
1
  import { Context } from '@unchainedshop/types/api';
2
2
 
3
3
  export default async function removeProduct(payload: any, { logger }, unchainedAPI: Context) {
4
- const { services } = unchainedAPI;
4
+ const { services, userId } = unchainedAPI;
5
5
  const { _id } = payload;
6
- logger.debug('remove product');
6
+ logger.debug(`remove product ${_id}`);
7
7
 
8
- await services.products.removeProduct({ productId: _id }, unchainedAPI);
8
+ await services.products.removeProduct({ productId: _id, userId }, unchainedAPI);
9
9
 
10
10
  return {
11
11
  entity: 'PRODUCT',
@@ -8,6 +8,10 @@ export default async function createProduct(payload: any, { logger, authorId },
8
8
  const { modules, userId } = unchainedAPI;
9
9
  const { specification, media, variations, _id } = payload;
10
10
 
11
+ if (!(await modules.products.productExists({ productId: _id }))) {
12
+ throw new Error(`Can't update non-existing product ${_id}`);
13
+ }
14
+
11
15
  if (specification) {
12
16
  const productData = transformSpecificationToProductStructure(specification);
13
17
  logger.debug('update product object', productData);
@@ -33,12 +37,6 @@ export default async function createProduct(payload: any, { logger, authorId },
33
37
  }
34
38
  }
35
39
 
36
- if (variations || media) {
37
- if (!(await modules.products.productExists({ productId: _id }))) {
38
- throw new Error(`Can't update non-existing product ${_id}`);
39
- }
40
- }
41
-
42
40
  if (variations) {
43
41
  logger.debug('replace variations', variations);
44
42
  await upsertVariations(
@@ -1,44 +1,7 @@
1
1
  import { Context } from '@unchainedshop/types/api';
2
- import { File } from '@unchainedshop/types/files';
3
2
  import { ProductMedia, ProductMediaText } from '@unchainedshop/types/products.media';
4
3
  import convertTagsToLowerCase from '../utils/convertTagsToLowerCase';
5
-
6
- const upsertAsset = async (
7
- asset: File & { fileName: string; headers?: Record<string, unknown> },
8
- unchainedAPI: Context,
9
- ) => {
10
- const { modules, services, userId } = unchainedAPI;
11
- const { _id, fileName, url, meta, headers, ...assetData } = asset;
12
- const fileId = _id;
13
-
14
- try {
15
- if (_id && (await modules.files.findFile({ fileId }))) throw new Error('Media already exists');
16
-
17
- const assetObject = await services.files.uploadFileFromURL(
18
- {
19
- directoryName: 'product-media',
20
- fileInput: {
21
- fileLink: url,
22
- fileName,
23
- headers,
24
- },
25
- meta: { ...meta, fileId },
26
- userId,
27
- },
28
- unchainedAPI,
29
- );
30
-
31
- if (!assetObject) throw new Error('Media not created');
32
- return assetObject;
33
- } catch (e) {
34
- if (fileId) {
35
- await modules.files.update(fileId, { meta: { ...meta, fileId }, ...assetData }, userId);
36
- const file = await modules.files.findFile({ fileId });
37
- return file;
38
- }
39
- return null;
40
- }
41
- };
4
+ import upsertAsset from '../../upsertAsset';
42
5
 
43
6
  const upsertProductMedia = async (productMedia: ProductMedia, { modules, userId }: Context) => {
44
7
  try {
@@ -58,6 +21,7 @@ export default async function upsertMedia({ media, authorId, productId }, unchai
58
21
  media.map(async ({ asset, content, ...mediaData }) => {
59
22
  const tags = convertTagsToLowerCase(mediaData?.tags);
60
23
  const file = await upsertAsset(
24
+ 'product-media',
61
25
  { meta: { ...(asset.meta || {}), productId }, ...asset },
62
26
  unchainedAPI,
63
27
  );
@@ -5,9 +5,6 @@ export default async function upsertProductContent(
5
5
  { productId, content, authorId },
6
6
  { modules, userId }: Context,
7
7
  ) {
8
- const product = await modules.products.findProduct({ productId });
9
- if (!product) throw new Error(`Can't update content of non-existing product ${productId}`);
10
-
11
8
  await Promise.all(
12
9
  Object.entries(content).map(
13
10
  async ([locale, { authorId: tAuthorId, ...localizedData }]: [string, ProductText]) => {
@@ -0,0 +1,45 @@
1
+ import { Context } from '@unchainedshop/types/api';
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: Context,
8
+ ) => {
9
+ const { modules, services, userId } = 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 }, userId);
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
+ userId,
34
+ },
35
+ unchainedAPI,
36
+ );
37
+
38
+ if (!assetObject) throw new Error('Media not created');
39
+ return assetObject;
40
+ } catch (e) {
41
+ return null;
42
+ }
43
+ };
44
+
45
+ export default upsertAsset;
@@ -1,6 +1,6 @@
1
- import { Context } from '@unchainedshop/types/api';
1
+ import { UnchainedCore } from '@unchainedshop/types/core';
2
2
 
3
- export default async (unchainedAPI: Context, 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 };
@@ -47,10 +47,8 @@ export const setupAccounts = (unchainedAPI: UnchainedCore) => {
47
47
  {
48
48
  email: params.email || `${guestname}@unchained.local`,
49
49
  guest: true,
50
- profile: {},
51
50
  password: null,
52
51
  initialPassword: true,
53
- lastBillingAddress: {},
54
52
  },
55
53
  {},
56
54
  );
@@ -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
- 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
  )
@@ -6,13 +6,10 @@ export const getOrderAttachmentsData = async (
6
6
  params: { fileType: string },
7
7
  { modules }: Context,
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);