@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.
- package/jest.config.js +9 -0
- package/lib/bulk-importer/handlers/assortment/create.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/create.js +5 -10
- package/lib/bulk-importer/handlers/assortment/create.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/remove.d.ts +2 -2
- package/lib/bulk-importer/handlers/assortment/remove.js +2 -2
- package/lib/bulk-importer/handlers/assortment/remove.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/update.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/update.js +4 -8
- package/lib/bulk-importer/handlers/assortment/update.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js +10 -7
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentChildren.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js +3 -3
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentContent.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.js +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentFilters.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js +8 -7
- package/lib/bulk-importer/handlers/assortment/upsertAssortmentProducts.js.map +1 -1
- package/lib/bulk-importer/handlers/assortment/upsertMedia.d.ts +3 -4
- package/lib/bulk-importer/handlers/assortment/upsertMedia.js +8 -38
- package/lib/bulk-importer/handlers/assortment/upsertMedia.js.map +1 -1
- package/lib/bulk-importer/handlers/filter/create.d.ts +3 -4
- package/lib/bulk-importer/handlers/filter/create.js +4 -6
- package/lib/bulk-importer/handlers/filter/create.js.map +1 -1
- package/lib/bulk-importer/handlers/filter/remove.d.ts +2 -2
- package/lib/bulk-importer/handlers/filter/remove.js +2 -2
- package/lib/bulk-importer/handlers/filter/remove.js.map +1 -1
- package/lib/bulk-importer/handlers/filter/update.d.ts +3 -4
- package/lib/bulk-importer/handlers/filter/update.js +3 -4
- package/lib/bulk-importer/handlers/filter/update.js.map +1 -1
- package/lib/bulk-importer/handlers/filter/upsertFilterContent.d.ts +3 -5
- package/lib/bulk-importer/handlers/filter/upsertFilterContent.js +3 -3
- package/lib/bulk-importer/handlers/filter/upsertFilterContent.js.map +1 -1
- package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.d.ts +2 -2
- package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js +2 -2
- package/lib/bulk-importer/handlers/filter/upsertFilterOptionContent.js.map +1 -1
- package/lib/bulk-importer/handlers/product/create.d.ts +3 -4
- package/lib/bulk-importer/handlers/product/create.js +5 -9
- package/lib/bulk-importer/handlers/product/create.js.map +1 -1
- package/lib/bulk-importer/handlers/product/remove.d.ts +2 -2
- package/lib/bulk-importer/handlers/product/remove.js +1 -1
- package/lib/bulk-importer/handlers/product/remove.js.map +1 -1
- package/lib/bulk-importer/handlers/product/update.d.ts +3 -4
- package/lib/bulk-importer/handlers/product/update.js +4 -7
- package/lib/bulk-importer/handlers/product/update.js.map +1 -1
- package/lib/bulk-importer/handlers/product/upsertMedia.d.ts +3 -4
- package/lib/bulk-importer/handlers/product/upsertMedia.js +8 -38
- package/lib/bulk-importer/handlers/product/upsertMedia.js.map +1 -1
- package/lib/bulk-importer/handlers/product/upsertProductContent.d.ts +3 -4
- package/lib/bulk-importer/handlers/product/upsertProductContent.js +3 -3
- package/lib/bulk-importer/handlers/product/upsertProductContent.js.map +1 -1
- package/lib/bulk-importer/handlers/product/upsertVariations.d.ts +3 -4
- package/lib/bulk-importer/handlers/product/upsertVariations.js +16 -13
- package/lib/bulk-importer/handlers/product/upsertVariations.js.map +1 -1
- package/lib/bulk-importer/upsertAsset.d.ts +7 -0
- package/lib/bulk-importer/upsertAsset.js +33 -0
- package/lib/bulk-importer/upsertAsset.js.map +1 -0
- package/lib/context/setAccessToken.js.map +1 -1
- package/lib/context/withAccessToken.d.ts +2 -1
- package/lib/context/withAccessToken.js.map +1 -1
- package/lib/platform-index.d.ts +4 -0
- package/lib/platform-index.js +3 -0
- package/lib/platform-index.js.map +1 -1
- package/lib/setup/setupAccounts.js +2 -3
- package/lib/setup/setupAccounts.js.map +1 -1
- package/lib/setup/setupCarts.js.map +1 -1
- package/lib/startPlatform.d.ts +6 -2
- package/lib/startPlatform.js +3 -5
- package/lib/startPlatform.js.map +1 -1
- package/lib/templates/resolveQuotationStatusTemplate.js +1 -1
- package/lib/templates/resolveQuotationStatusTemplate.js.map +1 -1
- package/lib/templates/utils/getOrderAttachmentsData.d.ts +2 -2
- package/lib/templates/utils/getOrderAttachmentsData.js +3 -10
- package/lib/templates/utils/getOrderAttachmentsData.js.map +1 -1
- package/lib/templates/utils/getOrderPositionsData.d.ts +2 -2
- package/lib/templates/utils/getOrderPositionsData.js.map +1 -1
- package/package.json +15 -14
- package/src/bulk-importer/handlers/assortment/create.ts +9 -18
- package/src/bulk-importer/handlers/assortment/remove.ts +4 -4
- package/src/bulk-importer/handlers/assortment/update.ts +5 -13
- package/src/bulk-importer/handlers/assortment/upsertAssortmentChildren.ts +10 -20
- package/src/bulk-importer/handlers/assortment/upsertAssortmentContent.ts +5 -12
- package/src/bulk-importer/handlers/assortment/upsertAssortmentFilters.ts +4 -5
- package/src/bulk-importer/handlers/assortment/upsertAssortmentProducts.ts +11 -20
- package/src/bulk-importer/handlers/assortment/upsertMedia.ts +15 -58
- package/src/bulk-importer/handlers/filter/create.ts +4 -8
- package/src/bulk-importer/handlers/filter/remove.ts +4 -4
- package/src/bulk-importer/handlers/filter/update.ts +4 -6
- package/src/bulk-importer/handlers/filter/upsertFilterContent.ts +5 -15
- package/src/bulk-importer/handlers/filter/upsertFilterOptionContent.ts +12 -12
- package/src/bulk-importer/handlers/product/create.ts +12 -23
- package/src/bulk-importer/handlers/product/remove.ts +3 -3
- package/src/bulk-importer/handlers/product/update.ts +7 -14
- package/src/bulk-importer/handlers/product/upsertMedia.ts +14 -54
- package/src/bulk-importer/handlers/product/upsertProductContent.ts +5 -15
- package/src/bulk-importer/handlers/product/upsertVariations.ts +26 -34
- package/src/bulk-importer/upsertAsset.ts +44 -0
- package/src/context/setAccessToken.ts +1 -1
- package/src/context/withAccessToken.ts +2 -1
- package/src/platform-index.ts +6 -0
- package/src/setup/setupAccounts.ts +7 -10
- package/src/setup/setupCarts.ts +1 -0
- package/src/startPlatform.ts +8 -8
- package/src/templates/resolveQuotationStatusTemplate.ts +2 -2
- package/src/templates/utils/getOrderAttachmentsData.ts +8 -15
- package/src/templates/utils/getOrderPositionsData.ts +2 -2
- package/tests/platform-index.test.ts +5 -0
|
@@ -1,48 +1,11 @@
|
|
|
1
|
-
import { Context } from '@unchainedshop/types/api';
|
|
2
1
|
import { AssortmentMediaText } from '@unchainedshop/types/assortments.media';
|
|
3
|
-
import {
|
|
2
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
3
|
+
import upsertAsset from '../../upsertAsset';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
unchainedAPI: Context,
|
|
8
|
-
) => {
|
|
9
|
-
const { modules, services, userId } = unchainedAPI;
|
|
10
|
-
const { _id, fileName, url, meta, headers, ...assetData } = asset;
|
|
11
|
-
const fileId = _id;
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
if (_id && (await modules.files.findFile({ fileId }))) throw new Error('Media already exists');
|
|
15
|
-
|
|
16
|
-
const assetObject = await services.files.uploadFileFromURL(
|
|
17
|
-
{
|
|
18
|
-
directoryName: 'assortment-media',
|
|
19
|
-
fileInput: {
|
|
20
|
-
fileLink: url,
|
|
21
|
-
fileName,
|
|
22
|
-
headers,
|
|
23
|
-
},
|
|
24
|
-
meta: { ...meta, fileId },
|
|
25
|
-
userId,
|
|
26
|
-
},
|
|
27
|
-
unchainedAPI,
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
if (!assetObject) throw new Error('Media not created');
|
|
31
|
-
return assetObject;
|
|
32
|
-
} catch (e) {
|
|
33
|
-
if (fileId) {
|
|
34
|
-
await modules.files.update(fileId, { meta: { ...meta, fileId }, ...assetData }, userId);
|
|
35
|
-
const file = await modules.files.findFile({ fileId });
|
|
36
|
-
return file;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const upsertMediaObject = async (media, unchainedAPI: Context) => {
|
|
43
|
-
const { modules, userId } = unchainedAPI;
|
|
5
|
+
const upsertMediaObject = async (media, unchainedAPI: UnchainedCore) => {
|
|
6
|
+
const { modules } = unchainedAPI;
|
|
44
7
|
try {
|
|
45
|
-
const assortmentMedia = await modules.assortments.media.create(media
|
|
8
|
+
const assortmentMedia = await modules.assortments.media.create(media);
|
|
46
9
|
return assortmentMedia;
|
|
47
10
|
} catch (e) {
|
|
48
11
|
const { _id, ...mediaData } = media;
|
|
@@ -50,11 +13,12 @@ const upsertMediaObject = async (media, unchainedAPI: Context) => {
|
|
|
50
13
|
}
|
|
51
14
|
};
|
|
52
15
|
|
|
53
|
-
export default async ({ media,
|
|
54
|
-
const { modules
|
|
16
|
+
export default async ({ media, assortmentId }, unchainedAPI: UnchainedCore) => {
|
|
17
|
+
const { modules } = unchainedAPI;
|
|
55
18
|
const mediaObjects = await Promise.all(
|
|
56
19
|
media.map(async ({ asset, content, ...mediaData }) => {
|
|
57
20
|
const file = await upsertAsset(
|
|
21
|
+
'assortment-media',
|
|
58
22
|
{ meta: { ...(asset.meta || {}), assortmentId }, ...asset },
|
|
59
23
|
unchainedAPI,
|
|
60
24
|
);
|
|
@@ -62,7 +26,6 @@ export default async ({ media, authorId, assortmentId }, unchainedAPI: Context)
|
|
|
62
26
|
|
|
63
27
|
const mediaObject = await upsertMediaObject(
|
|
64
28
|
{
|
|
65
|
-
authorId,
|
|
66
29
|
...mediaData,
|
|
67
30
|
assortmentId,
|
|
68
31
|
mediaId: file._id,
|
|
@@ -74,19 +37,13 @@ export default async ({ media, authorId, assortmentId }, unchainedAPI: Context)
|
|
|
74
37
|
|
|
75
38
|
if (content) {
|
|
76
39
|
await Promise.all(
|
|
77
|
-
Object.entries(content).map(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
locale,
|
|
85
|
-
localizedData,
|
|
86
|
-
tAuthorId || authorId || userId,
|
|
87
|
-
);
|
|
88
|
-
},
|
|
89
|
-
),
|
|
40
|
+
Object.entries(content).map(async ([locale, localizedData]: [string, AssortmentMediaText]) => {
|
|
41
|
+
return modules.assortments.media.texts.upsertLocalizedText(
|
|
42
|
+
mediaObject._id,
|
|
43
|
+
locale,
|
|
44
|
+
localizedData,
|
|
45
|
+
);
|
|
46
|
+
}),
|
|
90
47
|
);
|
|
91
48
|
}
|
|
92
49
|
return mediaObject;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import upsertFilterContent from './upsertFilterContent';
|
|
3
3
|
import upsertFilterOptionContent from './upsertFilterOptionContent';
|
|
4
4
|
|
|
5
5
|
export default async function createFilter(
|
|
6
6
|
payload: any,
|
|
7
|
-
{ logger,
|
|
8
|
-
unchainedAPI:
|
|
7
|
+
{ logger, createShouldUpsertIfIDExists },
|
|
8
|
+
unchainedAPI: UnchainedCore,
|
|
9
9
|
) {
|
|
10
10
|
const { modules } = unchainedAPI;
|
|
11
11
|
const { specification, _id } = payload;
|
|
@@ -23,11 +23,9 @@ export default async function createFilter(
|
|
|
23
23
|
...filterData,
|
|
24
24
|
_id,
|
|
25
25
|
options: options?.map((option) => option.value) || [],
|
|
26
|
-
authorId,
|
|
27
26
|
},
|
|
28
27
|
unchainedAPI,
|
|
29
28
|
{ skipInvalidation: true },
|
|
30
|
-
unchainedAPI.userId,
|
|
31
29
|
);
|
|
32
30
|
} catch (e) {
|
|
33
31
|
if (!createShouldUpsertIfIDExists) throw e;
|
|
@@ -38,11 +36,9 @@ export default async function createFilter(
|
|
|
38
36
|
{
|
|
39
37
|
...filterData,
|
|
40
38
|
options: options?.map((option) => option.value) || [],
|
|
41
|
-
authorId,
|
|
42
39
|
},
|
|
43
40
|
unchainedAPI,
|
|
44
41
|
{ skipInvalidation: true },
|
|
45
|
-
unchainedAPI.userId,
|
|
46
42
|
);
|
|
47
43
|
}
|
|
48
44
|
|
|
@@ -51,7 +47,7 @@ export default async function createFilter(
|
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
logger.debug('create localized content for filter', content);
|
|
54
|
-
await upsertFilterContent({ content, filterId: _id
|
|
50
|
+
await upsertFilterContent({ content, filterId: _id }, unchainedAPI);
|
|
55
51
|
|
|
56
52
|
logger.debug('create localized content for filter options', content);
|
|
57
53
|
await upsertFilterOptionContent({ options, filterId: _id }, unchainedAPI);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
|
|
3
|
-
export default async function removeFilter(payload: any, { logger }, unchainedAPI:
|
|
4
|
-
const { modules
|
|
3
|
+
export default async function removeFilter(payload: any, { logger }, unchainedAPI: UnchainedCore) {
|
|
4
|
+
const { modules } = unchainedAPI;
|
|
5
5
|
const { _id } = payload;
|
|
6
6
|
logger.debug('remove filter');
|
|
7
|
-
await modules.assortments.filters.deleteMany({ filterId: _id }
|
|
7
|
+
await modules.assortments.filters.deleteMany({ filterId: _id });
|
|
8
8
|
await modules.filters.delete(_id);
|
|
9
9
|
|
|
10
10
|
return {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import upsertFilterContent from './upsertFilterContent';
|
|
3
3
|
import upsertFilterOptionContent from './upsertFilterOptionContent';
|
|
4
4
|
|
|
5
5
|
export default async function updateFilter(
|
|
6
6
|
payload: any,
|
|
7
|
-
{
|
|
8
|
-
unchainedAPI:
|
|
7
|
+
{ logger }: { logger: any },
|
|
8
|
+
unchainedAPI: UnchainedCore,
|
|
9
9
|
) {
|
|
10
10
|
const { modules } = unchainedAPI;
|
|
11
11
|
const { specification, _id } = payload;
|
|
@@ -23,17 +23,15 @@ export default async function updateFilter(
|
|
|
23
23
|
{
|
|
24
24
|
...filterData,
|
|
25
25
|
options: options?.map((option) => option.value) || [],
|
|
26
|
-
authorId,
|
|
27
26
|
},
|
|
28
27
|
unchainedAPI,
|
|
29
28
|
{ skipInvalidation: true },
|
|
30
|
-
unchainedAPI.userId,
|
|
31
29
|
);
|
|
32
30
|
}
|
|
33
31
|
|
|
34
32
|
if (content) {
|
|
35
33
|
logger.debug('replace localized content for filter', content);
|
|
36
|
-
await upsertFilterContent({ content, filterId: _id
|
|
34
|
+
await upsertFilterContent({ content, filterId: _id }, unchainedAPI);
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
if (options) {
|
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import { FilterText } from '@unchainedshop/types/filters';
|
|
3
3
|
|
|
4
|
-
export default async function upsertFilterContent(
|
|
5
|
-
{ content, filterId, authorId },
|
|
6
|
-
unchainedAPI: Context,
|
|
7
|
-
) {
|
|
4
|
+
export default async function upsertFilterContent({ content, filterId }, unchainedAPI: UnchainedCore) {
|
|
8
5
|
return Promise.all(
|
|
9
|
-
Object.entries(content).map(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
{ filterId },
|
|
13
|
-
locale,
|
|
14
|
-
localizedData,
|
|
15
|
-
tAuthorId || authorId || unchainedAPI.userId,
|
|
16
|
-
);
|
|
17
|
-
},
|
|
18
|
-
),
|
|
6
|
+
Object.entries(content).map(async ([locale, localizedData]: [string, FilterText]) => {
|
|
7
|
+
return unchainedAPI.modules.filters.texts.upsertLocalizedText({ filterId }, locale, localizedData);
|
|
8
|
+
}),
|
|
19
9
|
);
|
|
20
10
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import { FilterText } from '@unchainedshop/types/filters';
|
|
3
3
|
|
|
4
|
-
export default async function upsertFilterOptionContent(
|
|
4
|
+
export default async function upsertFilterOptionContent(
|
|
5
|
+
{ options, filterId },
|
|
6
|
+
unchainedAPI: UnchainedCore,
|
|
7
|
+
) {
|
|
5
8
|
return Promise.all(
|
|
6
9
|
(options || []).map(async ({ content: optionContent, value: optionValue }) => {
|
|
7
10
|
await Promise.all(
|
|
8
|
-
Object.entries(optionContent).map(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
);
|
|
16
|
-
},
|
|
17
|
-
),
|
|
11
|
+
Object.entries(optionContent).map(async ([locale, localizedData]: [string, FilterText]) => {
|
|
12
|
+
return unchainedAPI.modules.filters.texts.upsertLocalizedText(
|
|
13
|
+
{ filterId, filterOptionValue: optionValue },
|
|
14
|
+
locale,
|
|
15
|
+
localizedData,
|
|
16
|
+
);
|
|
17
|
+
}),
|
|
18
18
|
);
|
|
19
19
|
}),
|
|
20
20
|
);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import upsertVariations from './upsertVariations';
|
|
3
3
|
import upsertMedia from './upsertMedia';
|
|
4
4
|
import upsertProductContent from './upsertProductContent';
|
|
@@ -6,10 +6,10 @@ import transformSpecificationToProductStructure from './transformSpecificationTo
|
|
|
6
6
|
|
|
7
7
|
export default async function createProduct(
|
|
8
8
|
payload: any,
|
|
9
|
-
{ logger,
|
|
10
|
-
unchainedAPI:
|
|
9
|
+
{ logger, createShouldUpsertIfIDExists },
|
|
10
|
+
unchainedAPI: UnchainedCore,
|
|
11
11
|
) {
|
|
12
|
-
const { modules
|
|
12
|
+
const { modules } = unchainedAPI;
|
|
13
13
|
const { specification, media, variations, _id } = payload;
|
|
14
14
|
|
|
15
15
|
if (!specification) throw new Error(`Specification is required when creating new product ${_id}`);
|
|
@@ -19,26 +19,17 @@ export default async function createProduct(
|
|
|
19
19
|
const productData = transformSpecificationToProductStructure(specification);
|
|
20
20
|
logger.debug('create product object', productData);
|
|
21
21
|
try {
|
|
22
|
-
await modules.products.create(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
authorId,
|
|
27
|
-
},
|
|
28
|
-
userId,
|
|
29
|
-
);
|
|
22
|
+
await modules.products.create({
|
|
23
|
+
...productData,
|
|
24
|
+
_id,
|
|
25
|
+
});
|
|
30
26
|
} catch (e) {
|
|
31
27
|
if (!createShouldUpsertIfIDExists) throw e;
|
|
32
28
|
|
|
33
29
|
logger.debug('entity already exists, falling back to update', specification);
|
|
34
|
-
await modules.products.update(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
...productData,
|
|
38
|
-
authorId,
|
|
39
|
-
},
|
|
40
|
-
userId,
|
|
41
|
-
);
|
|
30
|
+
await modules.products.update(_id, {
|
|
31
|
+
...productData,
|
|
32
|
+
});
|
|
42
33
|
}
|
|
43
34
|
|
|
44
35
|
if (!(await modules.products.productExists({ productId: _id }))) {
|
|
@@ -50,7 +41,6 @@ export default async function createProduct(
|
|
|
50
41
|
{
|
|
51
42
|
content: specification.content,
|
|
52
43
|
productId: _id,
|
|
53
|
-
authorId,
|
|
54
44
|
},
|
|
55
45
|
unchainedAPI,
|
|
56
46
|
);
|
|
@@ -60,13 +50,12 @@ export default async function createProduct(
|
|
|
60
50
|
{
|
|
61
51
|
variations: variations || [],
|
|
62
52
|
productId: _id,
|
|
63
|
-
authorId,
|
|
64
53
|
},
|
|
65
54
|
unchainedAPI,
|
|
66
55
|
);
|
|
67
56
|
|
|
68
57
|
logger.debug('create product media', media);
|
|
69
|
-
await upsertMedia({ media: media || [], productId: _id
|
|
58
|
+
await upsertMedia({ media: media || [], productId: _id }, unchainedAPI);
|
|
70
59
|
|
|
71
60
|
return {
|
|
72
61
|
entity: 'PRODUCT',
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
|
|
3
|
-
export default async function removeProduct(payload: any, { logger }, unchainedAPI:
|
|
3
|
+
export default async function removeProduct(payload: any, { logger }, unchainedAPI: UnchainedCore) {
|
|
4
4
|
const { services } = unchainedAPI;
|
|
5
5
|
const { _id } = payload;
|
|
6
|
-
logger.debug(
|
|
6
|
+
logger.debug(`remove product ${_id}`);
|
|
7
7
|
|
|
8
8
|
await services.products.removeProduct({ productId: _id }, unchainedAPI);
|
|
9
9
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import upsertVariations from './upsertVariations';
|
|
3
3
|
import upsertMedia from './upsertMedia';
|
|
4
4
|
import upsertProductContent from './upsertProductContent';
|
|
5
5
|
import transformSpecificationToProductStructure from './transformSpecificationToProductStructure';
|
|
6
6
|
|
|
7
|
-
export default async function createProduct(payload: any, { logger
|
|
8
|
-
const { modules
|
|
7
|
+
export default async function createProduct(payload: any, { logger }, unchainedAPI: UnchainedCore) {
|
|
8
|
+
const { modules } = unchainedAPI;
|
|
9
9
|
const { specification, media, variations, _id } = payload;
|
|
10
10
|
|
|
11
11
|
if (!(await modules.products.productExists({ productId: _id }))) {
|
|
@@ -15,14 +15,9 @@ export default async function createProduct(payload: any, { logger, authorId },
|
|
|
15
15
|
if (specification) {
|
|
16
16
|
const productData = transformSpecificationToProductStructure(specification);
|
|
17
17
|
logger.debug('update product object', productData);
|
|
18
|
-
await modules.products.update(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
...productData,
|
|
22
|
-
authorId,
|
|
23
|
-
},
|
|
24
|
-
userId,
|
|
25
|
-
);
|
|
18
|
+
await modules.products.update(_id, {
|
|
19
|
+
...productData,
|
|
20
|
+
});
|
|
26
21
|
|
|
27
22
|
if (specification.content) {
|
|
28
23
|
logger.debug('replace localized content for product', specification.content);
|
|
@@ -30,7 +25,6 @@ export default async function createProduct(payload: any, { logger, authorId },
|
|
|
30
25
|
{
|
|
31
26
|
content: specification.content,
|
|
32
27
|
productId: _id,
|
|
33
|
-
authorId,
|
|
34
28
|
},
|
|
35
29
|
unchainedAPI,
|
|
36
30
|
);
|
|
@@ -43,7 +37,6 @@ export default async function createProduct(payload: any, { logger, authorId },
|
|
|
43
37
|
{
|
|
44
38
|
variations: variations || [],
|
|
45
39
|
productId: _id,
|
|
46
|
-
authorId,
|
|
47
40
|
},
|
|
48
41
|
unchainedAPI,
|
|
49
42
|
);
|
|
@@ -51,7 +44,7 @@ export default async function createProduct(payload: any, { logger, authorId },
|
|
|
51
44
|
|
|
52
45
|
if (media) {
|
|
53
46
|
logger.debug('replace product media', media);
|
|
54
|
-
await upsertMedia({ media, productId: _id
|
|
47
|
+
await upsertMedia({ media, productId: _id }, unchainedAPI);
|
|
55
48
|
}
|
|
56
49
|
|
|
57
50
|
return {
|
|
@@ -1,48 +1,11 @@
|
|
|
1
|
-
import { Context } from '@unchainedshop/types/api';
|
|
2
|
-
import { File } from '@unchainedshop/types/files';
|
|
3
1
|
import { ProductMedia, ProductMediaText } from '@unchainedshop/types/products.media';
|
|
2
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
4
3
|
import convertTagsToLowerCase from '../utils/convertTagsToLowerCase';
|
|
4
|
+
import upsertAsset from '../../upsertAsset';
|
|
5
5
|
|
|
6
|
-
const
|
|
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
|
-
};
|
|
42
|
-
|
|
43
|
-
const upsertProductMedia = async (productMedia: ProductMedia, { modules, userId }: Context) => {
|
|
6
|
+
const upsertProductMedia = async (productMedia: ProductMedia, { modules }: UnchainedCore) => {
|
|
44
7
|
try {
|
|
45
|
-
const productMediaObj = await modules.products.media.create(productMedia
|
|
8
|
+
const productMediaObj = await modules.products.media.create(productMedia);
|
|
46
9
|
return productMediaObj;
|
|
47
10
|
} catch (e) {
|
|
48
11
|
const { _id, ...productMediaData } = productMedia;
|
|
@@ -51,13 +14,14 @@ const upsertProductMedia = async (productMedia: ProductMedia, { modules, userId
|
|
|
51
14
|
}
|
|
52
15
|
};
|
|
53
16
|
|
|
54
|
-
export default async function upsertMedia({ media,
|
|
55
|
-
const { modules
|
|
17
|
+
export default async function upsertMedia({ media, productId }, unchainedAPI: UnchainedCore) {
|
|
18
|
+
const { modules } = unchainedAPI;
|
|
56
19
|
|
|
57
20
|
const productMediaObjects = await Promise.all(
|
|
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
|
);
|
|
@@ -65,7 +29,6 @@ export default async function upsertMedia({ media, authorId, productId }, unchai
|
|
|
65
29
|
const fileId = file._id;
|
|
66
30
|
const productMedia = await upsertProductMedia(
|
|
67
31
|
{
|
|
68
|
-
authorId,
|
|
69
32
|
...mediaData,
|
|
70
33
|
tags,
|
|
71
34
|
productId,
|
|
@@ -77,16 +40,13 @@ export default async function upsertMedia({ media, authorId, productId }, unchai
|
|
|
77
40
|
|
|
78
41
|
if (content) {
|
|
79
42
|
await Promise.all(
|
|
80
|
-
Object.entries(content).map(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
},
|
|
89
|
-
),
|
|
43
|
+
Object.entries(content).map(async ([locale, localizedData]: [string, ProductMediaText]) => {
|
|
44
|
+
return modules.products.media.texts.upsertLocalizedText(
|
|
45
|
+
productMedia._id,
|
|
46
|
+
locale,
|
|
47
|
+
localizedData,
|
|
48
|
+
);
|
|
49
|
+
}),
|
|
90
50
|
);
|
|
91
51
|
}
|
|
92
52
|
return productMedia;
|
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import { ProductText } from '@unchainedshop/types/products';
|
|
3
3
|
|
|
4
|
-
export default async function upsertProductContent(
|
|
5
|
-
{ productId, content, authorId },
|
|
6
|
-
{ modules, userId }: Context,
|
|
7
|
-
) {
|
|
4
|
+
export default async function upsertProductContent({ productId, content }, { modules }: UnchainedCore) {
|
|
8
5
|
await Promise.all(
|
|
9
|
-
Object.entries(content).map(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
productId,
|
|
13
|
-
locale,
|
|
14
|
-
localizedData,
|
|
15
|
-
tAuthorId || authorId || userId,
|
|
16
|
-
);
|
|
17
|
-
},
|
|
18
|
-
),
|
|
6
|
+
Object.entries(content).map(async ([locale, localizedData]: [string, ProductText]) => {
|
|
7
|
+
return modules.products.texts.upsertLocalizedText(productId, locale, localizedData);
|
|
8
|
+
}),
|
|
19
9
|
);
|
|
20
10
|
}
|
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/types/core';
|
|
2
2
|
import { ProductVariation, ProductVariationText } from '@unchainedshop/types/products.variations';
|
|
3
3
|
|
|
4
|
-
const upsert = async (productVariation: ProductVariation, unchainedAPI:
|
|
5
|
-
const { modules
|
|
4
|
+
const upsert = async (productVariation: ProductVariation, unchainedAPI: UnchainedCore) => {
|
|
5
|
+
const { modules } = unchainedAPI;
|
|
6
6
|
try {
|
|
7
|
-
const newVariation = await modules.products.variations.create(productVariation
|
|
7
|
+
const newVariation = await modules.products.variations.create(productVariation);
|
|
8
8
|
return newVariation;
|
|
9
9
|
} catch (e) {
|
|
10
10
|
return modules.products.variations.update(productVariation._id, productVariation);
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
export default async function upsertVariations(
|
|
15
|
-
{
|
|
16
|
-
unchainedAPI: Context,
|
|
17
|
-
) {
|
|
18
|
-
const { modules, userId } = unchainedAPI;
|
|
14
|
+
export default async function upsertVariations({ variations, productId }, unchainedAPI: UnchainedCore) {
|
|
15
|
+
const { modules } = unchainedAPI;
|
|
19
16
|
|
|
20
|
-
const
|
|
17
|
+
const upsertedProductVariationIds = await Promise.all(
|
|
21
18
|
variations.map(async ({ content, options, ...variationsRest }) => {
|
|
22
19
|
const variation = await upsert(
|
|
23
20
|
{
|
|
24
|
-
authorId,
|
|
25
21
|
...variationsRest,
|
|
26
22
|
options: options.map((option) => option.value),
|
|
27
23
|
productId,
|
|
@@ -33,10 +29,7 @@ export default async function upsertVariations(
|
|
|
33
29
|
options.map(async ({ content: optionContent, value: optionValue }) => {
|
|
34
30
|
await Promise.all(
|
|
35
31
|
Object.entries(optionContent).map(
|
|
36
|
-
async ([locale,
|
|
37
|
-
string,
|
|
38
|
-
ProductVariationText,
|
|
39
|
-
]) => {
|
|
32
|
+
async ([locale, localizedData]: [string, ProductVariationText]) => {
|
|
40
33
|
return modules.products.variations.texts.upsertLocalizedText(
|
|
41
34
|
{
|
|
42
35
|
productVariationId: variation._id,
|
|
@@ -44,7 +37,6 @@ export default async function upsertVariations(
|
|
|
44
37
|
},
|
|
45
38
|
locale,
|
|
46
39
|
localizedData,
|
|
47
|
-
tAuthorId || authorId || userId,
|
|
48
40
|
);
|
|
49
41
|
},
|
|
50
42
|
),
|
|
@@ -52,27 +44,27 @@ export default async function upsertVariations(
|
|
|
52
44
|
}),
|
|
53
45
|
);
|
|
54
46
|
await Promise.all(
|
|
55
|
-
Object.entries(content).map(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
locale,
|
|
65
|
-
localizedData,
|
|
66
|
-
tAuthorId || authorId || userId,
|
|
67
|
-
);
|
|
68
|
-
},
|
|
69
|
-
),
|
|
47
|
+
Object.entries(content).map(async ([locale, localizedData]: [string, ProductVariationText]) => {
|
|
48
|
+
return modules.products.variations.texts.upsertLocalizedText(
|
|
49
|
+
{
|
|
50
|
+
productVariationId: variation._id,
|
|
51
|
+
},
|
|
52
|
+
locale,
|
|
53
|
+
localizedData,
|
|
54
|
+
);
|
|
55
|
+
}),
|
|
70
56
|
);
|
|
71
|
-
return variation;
|
|
57
|
+
return variation._id;
|
|
72
58
|
}),
|
|
73
59
|
);
|
|
74
|
-
await modules.products.variations.
|
|
60
|
+
const allVariations = await modules.products.variations.findProductVariations({
|
|
75
61
|
productId,
|
|
76
|
-
excludedProductVariationIds: productVariationObjects.map((obj) => obj._id),
|
|
77
62
|
});
|
|
63
|
+
await Promise.all(
|
|
64
|
+
allVariations.map(async (variation) => {
|
|
65
|
+
if (!upsertedProductVariationIds.includes(variation._id)) {
|
|
66
|
+
await modules.products.variations.delete(variation._id);
|
|
67
|
+
}
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
78
70
|
}
|