From 943f840b7df3b37defaed924c82788f1277e8f5b Mon Sep 17 00:00:00 2001 From: Florian Robel Date: Fri, 6 Dec 2024 10:32:21 +0100 Subject: [PATCH] feat: add analytics assignment and creation of sales channel analytics --- src/services/TestDataService.ts | 69 +++++++++++++++++++ src/types/ShopwareTypes.ts | 4 ++ tests/TestDataService/TestDataService.spec.ts | 9 +++ 3 files changed, 82 insertions(+) diff --git a/src/services/TestDataService.ts b/src/services/TestDataService.ts index d90b976..5ee00dc 100644 --- a/src/services/TestDataService.ts +++ b/src/services/TestDataService.ts @@ -28,6 +28,7 @@ import type { Country, CustomerGroup, SystemConfig, + SalesChannelAnalytics, } from '../types/ShopwareTypes'; import { expect } from '@playwright/test'; @@ -882,6 +883,31 @@ export class TestDataService { return systemConfigEntry; } + /** + * Creates a random sales channel analytics entity + * + * @param overrides - Specific data overrides that will be applied to the sales channel analytics data struct. + */ + async createSalesChannelAnalytics( + overrides: Partial = {}, + ): Promise { + + const basicSalesChannelAnalyticsStruct = this.getSalesChannelAnalyticsStruct(overrides); + + + const response = await this.AdminApiClient.post('sales-channel-analytics?_response=detail', { + data: basicSalesChannelAnalyticsStruct, + }); + expect(response.ok()).toBeTruthy(); + + const { data: salesChannelAnalytics } = (await response.json()) as { data: SalesChannelAnalytics }; + + this.addCreatedRecord('sales_channel_analytics', salesChannelAnalytics.id); + + return salesChannelAnalytics; + } + + /** * Assigns a media resource as the download of a digital product. * @@ -1084,6 +1110,35 @@ export class TestDataService { return salesChannel; } + /** + * Assigns a sales channel analytics entity to a sales channel. + * + * @param salesChannelId - The uuid of the sales channel. + * @param salesChannelAnalyticsId - The uuid of the sales channel analytics entity. + */ + async assignSalesChannelAnalytics(salesChannelId: string, salesChannelAnalyticsId: string) { + + const syncSalesChannelResponse = await this.AdminApiClient.post('./_action/sync', { + data: { + 'write-sales-channel': { + entity: 'sales_channel', + action: 'upsert', + payload: [ + { + id: salesChannelId, + analyticsId: salesChannelAnalyticsId, + }, + ], + }, + }, + }); + expect(syncSalesChannelResponse.ok()).toBeTruthy(); + + const { data: salesChannel } = await syncSalesChannelResponse.json(); + + return salesChannel; + } + /** * Assigns a country to a sales channel. * @@ -2237,6 +2292,20 @@ export class TestDataService { return Object.assign({}, basicCustomerGroup, overrides); } + getSalesChannelAnalyticsStruct(overrides: Partial = {}): Partial { + const salesChannelAnalyticsUuid = this.IdProvider.getIdPair().uuid; + const trackingId = this.IdProvider.getIdPair().id; + + const basicSalesChannelAnalyticsStruct = { + id: salesChannelAnalyticsUuid, + trackingId: trackingId, + active: true, + trackOrders: false, + anonymizeIp: true, + }; + return Object.assign({}, basicSalesChannelAnalyticsStruct, overrides); + } + async clearCaches() { await this.AdminApiClient.delete('_action/cache'); } diff --git a/src/types/ShopwareTypes.ts b/src/types/ShopwareTypes.ts index e59f628..bd4b388 100644 --- a/src/types/ShopwareTypes.ts +++ b/src/types/ShopwareTypes.ts @@ -190,6 +190,10 @@ export type CustomerGroup = components['schemas']['CustomerGroup'] & { id: string, }; +export type SalesChannelAnalytics = components['schemas']['SalesChannelAnalytics'] & { + id: string, +}; + export interface RegistrationData { salutation: string; firstName: string; diff --git a/tests/TestDataService/TestDataService.spec.ts b/tests/TestDataService/TestDataService.spec.ts index a85282f..750d07a 100644 --- a/tests/TestDataService/TestDataService.spec.ts +++ b/tests/TestDataService/TestDataService.spec.ts @@ -13,6 +13,7 @@ import { Category, APIResponse, SystemConfig, + SalesChannelAnalytics, } from '../../src'; test('Data Service', async ({ @@ -90,6 +91,9 @@ test('Data Service', async ({ expect(systemConfigEntry.configurationKey).toEqual('test.random.foo'); expect(systemConfigEntry.configurationValue).toEqual(true); + const salesChannelAnalytics = await TestDataService.createSalesChannelAnalytics({ active: false }); + expect(salesChannelAnalytics.active).toEqual(false); + // Test data clean-up with deactivated cleansing process TestDataService.setCleanUp(false); const cleanUpFalseResponse = await TestDataService.cleanUp(); @@ -143,6 +147,10 @@ test('Data Service', async ({ const { data: databaseSystemConfigEntry } = (await systemConfigEntryResponse.json()) as { data: SystemConfig }; expect(databaseSystemConfigEntry.id).toBe(systemConfigEntry.id); + const salesChannelAnalyticsResponse = await AdminApiContext.get(`./sales-channel-analytics/${salesChannelAnalytics.id}?_response=detail`); + const { data: databaseSalesChannelAnalytics } = (await salesChannelAnalyticsResponse.json()) as { data: SalesChannelAnalytics }; + expect(databaseSalesChannelAnalytics.id).toBe(salesChannelAnalytics.id); + // Test data clean-up with activated cleansing process TestDataService.setCleanUp(true); const cleanUpResponse = await TestDataService.cleanUp() as APIResponse; @@ -164,4 +172,5 @@ test('Data Service', async ({ expect(cleanUp['deleted']['product_manufacturer']).toBeDefined(); expect(cleanUp['deleted']['cms_page']).toBeDefined(); expect(cleanUp['deleted']['system_config']).toBeDefined(); + expect(cleanUp['deleted']['sales_channel_analytics']).toBeDefined(); });