From 8db37aa6fca7e8d34d3dd92c318bcdf3f7f65154 Mon Sep 17 00:00:00 2001 From: Vilsol Date: Wed, 12 Jun 2024 12:53:20 +0300 Subject: [PATCH] feat: seed accounts from file --- .nvmrc | 2 +- src/service.ts | 4 +++ src/worker.ts | 43 +++++++++++++++++++++++++++++- test/cfg/config.json | 3 ++- test/seed-accounts.json | 58 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 test/seed-accounts.json diff --git a/.nvmrc b/.nvmrc index ee5c2446..44e031ba 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -22.1.0 +22.2.0 diff --git a/src/service.ts b/src/service.ts index 35783736..a4d6f16b 100644 --- a/src/service.ts +++ b/src/service.ts @@ -565,6 +565,10 @@ export class UserService extends ServiceBase impleme return super.read(request, context); } + superUpsert(request: UserList, context: any): Promise> { + return super.upsert(request, context); + } + /** * Extends ServiceBase.create() */ diff --git a/src/worker.ts b/src/worker.ts index a1adb9bb..e589318a 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -17,7 +17,7 @@ import { BindConfig } from '@restorecommerce/chassis-srv/lib/microservice/transp import { HealthDefinition } from '@restorecommerce/rc-grpc-clients/dist/generated-server/grpc/health/v1/health.js'; import { UserServiceDefinition, - protoMetadata as userMeta + protoMetadata as userMeta, UserList } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/user.js'; import { protoMetadata as jobMeta @@ -349,6 +349,47 @@ export class Worker { }) } as BindConfig); + // Import static seed accounts + const seedAccountFile = this.cfg.get('seed_account_file'); + if (seedAccountFile) { + this.logger.info('Loading seed account file:', seedAccountFile); + await new Promise((resolve, reject) => { + fs.readFile(seedAccountFile, (err, data) => { + if (err) { + this.logger.error('Failed loading seed account file:', err); + reject(err); + return; + } + + const seedAccounts = JSON.parse(data.toString()); + this.logger.info(`Loaded ${seedAccounts.length} seed accounts`); + + const defaultSeedAccount = seedAccounts.find((p: any) => p?.tokens?.find((t: any) => t?.default_technical_token)); + if (!defaultSeedAccount) { + logger.error('Failed to find default seed account'); + reject(new Error('Failed to find default seed account')); + } + + this.userService.superUpsert(UserList.fromPartial({ + items: seedAccounts, + }), undefined) + .then(() => { + this.logger.info('Loading default seed account token into redis'); + + const token = defaultSeedAccount.tokens.find((t: any) => t.default_technical_token).token; + + cis.redisClient.set('default_account_api_token', token) + .then(() => resolve()) + .catch(reject); + }) + .catch(err => { + this.logger.error('Failed upserting seed account file:', err); + reject(err); + }); + }); + }); + } + // Start server await server.start(); diff --git a/test/cfg/config.json b/test/cfg/config.json index 23e97916..31a693c4 100644 --- a/test/cfg/config.json +++ b/test/cfg/config.json @@ -372,5 +372,6 @@ "access_token_path": "https://oauth2.googleapis.com/token" } } - } + }, + "seed_account_file": "./test/seed-accounts.json" } diff --git a/test/seed-accounts.json b/test/seed-accounts.json new file mode 100644 index 00000000..780f02e1 --- /dev/null +++ b/test/seed-accounts.json @@ -0,0 +1,58 @@ +[ + { + "id": "tech-user", + "name": "tech-user", + "firstName": "Tech", + "lastName": "User", + "email": "tech-user@restorecommerce.io", + "defaultScope": "", + "roleAssociations": [ + { + "id": "superadministrator-r-role-assoc-id", + "role": "superadministrator-r-id", + "attributes": [] + } + ], + "localeId": "de-de", + "timezoneId": "europe-berlin", + "meta": { + "owners": [ + { + "id": "urn:restorecommerce:acs:names:ownerIndicatoryEntity", + "value": "urn:restorecommerce:acs:model:organization.Organization", + "attributes": [ + { + "id": "urn:restorecommerce:acs:names:ownerInstance", + "value": "system" + } + ] + }, + { + "id": "urn:restorecommerce:acs:names:ownerIndicatoryEntity", + "value": "urn:restorecommerce:acs:model:user.User", + "attributes": [ + { + "id": "urn:restorecommerce:acs:names:ownerInstance", + "value": "tech-user" + } + ] + } + ] + }, + "active": true, + "password": "10cdd55a-3371-4ae3-919d-2506e78c0c33", + "guest": false, + "userType": "TECHNICAL_USER", + "invite": false, + "tokens": [ + { + "name": "access-token", + "token": "609edc0a-75d1-403e-9f9f-ad61bf42937c", + "scopes": [ + "superadministrator-r-role-assoc-id" + ], + "default_technical_token": true + } + ] + } +]