Skip to content

Commit

Permalink
feat: seed accounts from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jun 12, 2024
1 parent d2a34d9 commit 8db37aa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22.1.0
22.2.0
4 changes: 4 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ export class UserService extends ServiceBase<UserListResponse, UserList> impleme
return super.read(request, context);
}

superUpsert(request: UserList, context: any): Promise<DeepPartial<UserListResponse>> {
return super.upsert(request, context);
}

/**
* Extends ServiceBase.create()
*/
Expand Down
43 changes: 42 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -349,6 +349,47 @@ export class Worker {
})
} as BindConfig<HealthDefinition>);

// 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<void>((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();

Expand Down
3 changes: 2 additions & 1 deletion test/cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,6 @@
"access_token_path": "https://oauth2.googleapis.com/token"
}
}
}
},
"seed_account_file": "./test/seed-accounts.json"
}
58 changes: 58 additions & 0 deletions test/seed-accounts.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]

0 comments on commit 8db37aa

Please sign in to comment.