From 9ec432beddbd04fffab759f5de90691e15593e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Sefa=20Bal=C4=B1k?= Date: Mon, 25 Nov 2019 19:56:02 +0300 Subject: [PATCH] configuration gateway is implemented --- .vscode/launch.json | 21 ++++++++++ src/configuration/configuration.gateway.ts | 49 ++++++++++++++++++++-- src/configuration/configuration.service.ts | 5 +-- 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..06c9b76 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/src/main.ts", + "preLaunchTask": "tsc: build - tsconfig.json", + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/src/configuration/configuration.gateway.ts b/src/configuration/configuration.gateway.ts index ae22cc8..d40fcf8 100644 --- a/src/configuration/configuration.gateway.ts +++ b/src/configuration/configuration.gateway.ts @@ -12,7 +12,7 @@ export class ConfigurationGateway { @SubscribeMessage('panel.configurations.storefronts.get') async getStorefrontConfigsForPanel() { const configs = await this.configurationService.getAll(PlatformTypes.Storefront); - return { event: `panel.configurations.storefronts`, data: configs }; + return { event: 'panel.configurations.storefronts', data: configs }; } @SubscribeMessage('panel.configurations.gateways.get') @@ -22,10 +22,51 @@ export class ConfigurationGateway { } @SubscribeMessage('panel.configurations.storefront.add') - async addStorefrontConfigs(@MessageBody() body: { configurations: Configuration[], name: string }) { + async addStorefrontConfigs(@MessageBody() body: { configurations: Configuration, name: string }) { if (!body.configurations) return null; - const configs = await this.configurationService.add(PlatformTypes.Storefront, body.name, body.configurations); - this.server.emit(`panel.configurations.storefront.${body.name}`, configs); + await this.configurationService.add(PlatformTypes.Storefront, body.name, body.configurations); + this.server.emit('panel.configurations.storefronts', await this.configurationService.getAll(PlatformTypes.Storefront)); + this.server.emit(`configurations.storefront.update`, body.configurations); + } + + @SubscribeMessage('panel.configurations.gateway.add') + async addGatewayConfigs(@MessageBody() body: { configurations: Configuration, name: string }) { + if (!body.configurations) return null; + await this.configurationService.add(PlatformTypes.Gateway, body.name, body.configurations); + this.server.emit('panel.configurations.gateways', await this.configurationService.getAll(PlatformTypes.Gateway)); + this.server.emit(`configurations.gateway.update`, body.configurations); + } + + @SubscribeMessage('panel.configurations.storefront.update') + async updateStorefrontConfigs(@MessageBody() body: { configurations: Configuration, name: string }) { + if (!body.configurations) return null; + await this.configurationService.update(PlatformTypes.Storefront, body.name, body.configurations); + this.server.emit('panel.configurations.storefronts', await this.configurationService.getAll(PlatformTypes.Storefront)); + this.server.emit(`configurations.storefront.update`, body.configurations); + } + + @SubscribeMessage('panel.configurations.gateway.update') + async updateGatewayConfigs(@MessageBody() body: { configurations: Configuration, name: string }) { + if (!body.configurations) return null; + await this.configurationService.update(PlatformTypes.Gateway, body.name, body.configurations); + this.server.emit('panel.configurations.gateways', await this.configurationService.getAll(PlatformTypes.Gateway)); + this.server.emit(`configurations.gateway.update`, body.configurations); + } + + @SubscribeMessage('panel.configurations.storefront.delete') + async deleteStorefrontConfigs(@MessageBody() body: { name: string }) { + if (!body.name) return null; + await this.configurationService.delete(PlatformTypes.Storefront, body.name); + this.server.emit('panel.configurations.storefronts', await this.configurationService.getAll(PlatformTypes.Storefront)); + this.server.emit(`configurations.storefront.delete`, body.name); + } + + @SubscribeMessage('panel.configurations.gateway.delete') + async deleteGatewayConfigs(@MessageBody() body: { name: string }) { + if (!body.name) return null; + await this.configurationService.delete(PlatformTypes.Gateway, body.name); + this.server.emit('panel.configurations.gateways', await this.configurationService.getAll(PlatformTypes.Gateway)); + this.server.emit(`configurations.gateway.delete`, body.name); } @SubscribeMessage('configurations.storefront.get') diff --git a/src/configuration/configuration.service.ts b/src/configuration/configuration.service.ts index 7e27917..43086b3 100644 --- a/src/configuration/configuration.service.ts +++ b/src/configuration/configuration.service.ts @@ -8,7 +8,7 @@ import { PlatformTypes } from '../enums'; export class ConfigurationService { constructor(private readonly couchbaseService: CouchbaseService) { } - add(type: PlatformTypes, name: string, configuration: Configuration[]) { + add(type: PlatformTypes, name: string, configuration: Configuration) { return new Promise((resolve, reject) => { this.couchbaseService.getBucket().insert(`configuration_${type}_${name}`, {configuration, type: `configuration_${type}`}, (err, data) => { if (err) return reject(null); @@ -39,7 +39,6 @@ export class ConfigurationService { return new Promise((resolve, reject) => { this.couchbaseService.getBucket().get(`configuration_${type}_${name}`, (err, data) => { if (err) return reject(null); - console.log("get data: ", data); resolve(data.map((g) => g.value)); }); }); @@ -52,7 +51,7 @@ export class ConfigurationService { .stale(ViewQuery.Update.BEFORE); this.couchbaseService.getBucket().query(query, (err, data) => { if (err) return reject(null); - resolve(data.map((g) => g.value)); + resolve(data.map((g) => g.value?.configuration)); }); }); }