Skip to content

Commit 00e4844

Browse files
Configuration module is added
1 parent f9a30a9 commit 00e4844

File tree

9 files changed

+173
-0
lines changed

9 files changed

+173
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Configurations
2+
3+
### Gateway Configuration
4+
5+
#### Get Configurations of a Gateway
6+
7+
```
8+
const gatewayName = 'customGatewayName';
9+
socket.on(`configurations.gateway.${gatewayName}`, response => console.log('gateway configs: ', response.data));
10+
socket.emit('configurations.gateway.get', {name: gatewayName});
11+
```
12+
13+
#### Get Configurations of a Storefront
14+
15+
```
16+
const storefrontName = 'customStorefrontName';
17+
socket.on(`configurations.storefront.${storefrontName}`, response => console.log('storefront configs: ', response.data));
18+
socket.emit('configurations.storefront.get', {name: storefrontName});
19+
```

src/app.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { FragmentModule } from './fragment/fragment.module';
55
import { ServeStaticModule } from '@nestjs/serve-static';
66
import { dirname } from 'path';
77
import { CouchbaseModule } from './couchbase/couchbase.module';
8+
import { ConfigurationModule } from './configuration/configuration.module';
9+
import { ConfigurationGateway } from './configuration/configuration.gateway';
810

911
const sentryUi = dirname(require.resolve('@puzzle-js/sentry-ui'));
1012

@@ -17,6 +19,7 @@ const sentryUi = dirname(require.resolve('@puzzle-js/sentry-ui'));
1719
PageModule,
1820
FragmentModule,
1921
CouchbaseModule,
22+
ConfigurationModule,
2023
],
2124
})
2225
export class AppModule { }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { SubscribeMessage, WebSocketGateway, MessageBody, WebSocketServer } from '@nestjs/websockets';
2+
import { ConfigurationService } from './configuration.service';
3+
import { Server } from 'socket.io';
4+
import { Configuration } from './configuration.interface';
5+
import { PlatformTypes } from '../enums';
6+
7+
@WebSocketGateway()
8+
export class ConfigurationGateway {
9+
constructor(private readonly configurationService: ConfigurationService) { }
10+
@WebSocketServer() server: Server;
11+
12+
@SubscribeMessage('panel.configurations.storefronts.get')
13+
async getStorefrontConfigsForPanel() {
14+
const configs = await this.configurationService.getAll(PlatformTypes.Storefront);
15+
return { event: `panel.configurations.storefronts`, data: configs };
16+
}
17+
18+
@SubscribeMessage('panel.configurations.gateways.get')
19+
async getGatewayConfigsForPanel() {
20+
const configs = await this.configurationService.getAll(PlatformTypes.Gateway);
21+
return { event: `panel.configurations.gateways`, data: configs };
22+
}
23+
24+
@SubscribeMessage('panel.configurations.storefront.add')
25+
async addStorefrontConfigs(@MessageBody() body: { configurations: Configuration[], name: string }) {
26+
if (!body.configurations) return null;
27+
const configs = await this.configurationService.add(PlatformTypes.Storefront, body.name, body.configurations);
28+
this.server.emit(`panel.configurations.storefront.${body.name}`, configs);
29+
}
30+
31+
@SubscribeMessage('configurations.storefront.get')
32+
async getStorefrontConfigs(@MessageBody() body: { name: string }) {
33+
if (!body.name) return null;
34+
const configs = await this.configurationService.get(PlatformTypes.Storefront, body.name);
35+
return { event: `configurations.storefront.${body.name}`, data: configs };
36+
}
37+
38+
@SubscribeMessage('configurations.gateway.get')
39+
async getGatewayConfigs(@MessageBody() body: { name: string }) {
40+
if (!body.name) return null;
41+
const configs = await this.configurationService.get(PlatformTypes.Gateway, body.name);
42+
return { event: `configurations.gateway.${body.name}`, data: configs };
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Configuration = Record<string, string>;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Module } from '@nestjs/common';
2+
import { ConfigurationService } from './configuration.service';
3+
import { ConfigurationGateway } from './configuration.gateway';
4+
5+
@Module({
6+
providers: [ConfigurationGateway, ConfigurationService],
7+
})
8+
export class ConfigurationModule {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { CouchbaseService } from '../couchbase/couchbase.service';
3+
import { Configuration } from './configuration.interface';
4+
import { ViewQuery } from 'couchbase';
5+
6+
@Injectable()
7+
export class ConfigurationService {
8+
constructor(private readonly couchbaseService: CouchbaseService) { }
9+
10+
add(type: PlatformTypes, name: string, configuration: Configuration[]) {
11+
return new Promise((resolve, reject) => {
12+
this.couchbaseService.getBucket().insert(`configuration_${type}_${name}`, {configuration, type: `configuration_${type}`}, (err, data) => {
13+
if (err) return reject(null);
14+
resolve(data);
15+
});
16+
});
17+
}
18+
19+
delete(type: PlatformTypes, name: string) {
20+
return new Promise((resolve, reject) => {
21+
this.couchbaseService.getBucket().remove(`configuration_${type}_${name}`, (err, data) => {
22+
if (err) return reject(null);
23+
resolve(data);
24+
});
25+
});
26+
}
27+
28+
update(type: PlatformTypes, name: string, configuration: Configuration) {
29+
return new Promise((resolve, reject) => {
30+
this.couchbaseService.getBucket().upsert(`configuration_${type}_${name}`, {configuration, type: `configuration_${type}`}, (err, data) => {
31+
if (err) return reject(null);
32+
resolve(data);
33+
});
34+
});
35+
}
36+
37+
get(type: PlatformTypes, name: string) {
38+
return new Promise((resolve, reject) => {
39+
this.couchbaseService.getBucket().get(`configuration_${type}_${name}`, (err, data) => {
40+
if (err) return reject(null);
41+
console.log("get data: ", data);
42+
resolve(data.map((g) => g.value));
43+
});
44+
});
45+
}
46+
47+
getAll(type: PlatformTypes) {
48+
return new Promise((resolve, reject) => {
49+
const query = ViewQuery
50+
.from(`configuration_${type}`, 'getAll')
51+
.stale(ViewQuery.Update.BEFORE);
52+
this.couchbaseService.getBucket().query(query, (err, data) => {
53+
if (err) return reject(null);
54+
resolve(data.map((g) => g.value));
55+
});
56+
});
57+
}
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ConfigurationGateway } from '../configuration.gateway';
3+
4+
describe('ConfigurationGateway', () => {
5+
let gateway: ConfigurationGateway;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [ConfigurationGateway],
10+
}).compile();
11+
12+
gateway = module.get<ConfigurationGateway>(ConfigurationGateway);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(gateway).toBeDefined();
17+
});
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { ConfigurationService } from '../configuration.service';
3+
4+
describe('ConfigurationService', () => {
5+
let service: ConfigurationService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [ConfigurationService],
10+
}).compile();
11+
12+
service = module.get<ConfigurationService>(ConfigurationService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/enums.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum PlatformTypes {
2+
Storefront = 'storefront',
3+
Gateway = 'gateway',
4+
}

0 commit comments

Comments
 (0)