Skip to content

Commit 4bfdf52

Browse files
committed
feat: add groupByAccount
1 parent fcffb39 commit 4bfdf52

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed
Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { SSMClient } from '@aws-sdk/client-ssm'
22
import { get } from '@bifravst/aws-ssm-settings-helpers'
3-
import { getAccountsFromAllSettings } from './getAllAccounts.js'
4-
import { validateSettings, type Settings } from './settings.js'
53
import { NRFCLOUD_ACCOUNT_SCOPE } from './scope.js'
4+
import { groupByAccount } from './groupByAccount.js'
5+
import { validateSettings, type Settings } from './settings.js'
66

77
/**
88
* Returns settings for all accounts
@@ -13,34 +13,18 @@ export const getAllAccountsSettings = async ({
1313
}: {
1414
ssm: SSMClient
1515
stackName: string
16-
}): Promise<Record<string, Settings>> => {
17-
const allSettings = await get(ssm)({
18-
stackName,
19-
scope: NRFCLOUD_ACCOUNT_SCOPE,
20-
})()
21-
22-
const accounts = getAccountsFromAllSettings(allSettings)
23-
24-
return [...accounts].reduce(
25-
(allAccountSettings, account) => ({
26-
...allAccountSettings,
27-
[account]: validateSettings(
28-
Object.entries(allSettings)
29-
.filter(([k]) => k.startsWith(`${account}/`))
30-
.map<[string, string]>(([k, v]) => [
31-
k.replace(new RegExp(`^${account}/`), ''),
32-
v,
33-
])
34-
.reduce((s, [k, v]) => ({ ...s, [k]: v }), {}),
35-
),
16+
}): Promise<Record<string, Settings>> =>
17+
Object.entries(
18+
groupByAccount(
19+
await get(ssm)({
20+
stackName,
21+
scope: NRFCLOUD_ACCOUNT_SCOPE,
22+
})(),
23+
),
24+
).reduce(
25+
(allSettings, [account, settings]) => ({
26+
...allSettings,
27+
[account]: validateSettings(settings),
3628
}),
3729
{},
3830
)
39-
}
40-
41-
console.log(
42-
await getAllAccountsSettings({
43-
ssm: new SSMClient(),
44-
stackName: 'hello-nrfcloud-backend',
45-
}),
46-
)

src/settings/groupByAccount.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { it, describe } from 'node:test'
2+
import assert from 'node:assert/strict'
3+
import { groupByAccount } from './groupByAccount.js'
4+
5+
void describe('groupByAccount()', () => {
6+
void it('should group settings by account', () =>
7+
assert.deepEqual(
8+
groupByAccount({
9+
'elite/accountDeviceClientId':
10+
'account-4db55163-7593-4d63-9679-d16fbc2d464f',
11+
'elite/healthCheckModel': 'PCA20035+solar',
12+
'acme/mqttEndpoint': 'mqtt.nrfcloud.com',
13+
'acme/teamId': 'f4ba6ede-7867-43eb-a495-7e0de108f52e',
14+
}),
15+
{
16+
acme: {
17+
mqttEndpoint: 'mqtt.nrfcloud.com',
18+
teamId: 'f4ba6ede-7867-43eb-a495-7e0de108f52e',
19+
},
20+
elite: {
21+
accountDeviceClientId: 'account-4db55163-7593-4d63-9679-d16fbc2d464f',
22+
healthCheckModel: 'PCA20035+solar',
23+
},
24+
},
25+
))
26+
})

src/settings/groupByAccount.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getAccountsFromAllSettings } from './getAllAccounts.js'
2+
3+
export const groupByAccount = (
4+
allSettings: Record<string, string>,
5+
): Record<string, Record<string, string>> => {
6+
const accounts = getAccountsFromAllSettings(allSettings)
7+
8+
return [...accounts].reduce(
9+
(allAccountSettings, account) => ({
10+
...allAccountSettings,
11+
[account]: Object.entries(allSettings)
12+
.filter(([k]) => k.startsWith(`${account}/`))
13+
.map<[string, string]>(([k, v]) => [
14+
k.replace(new RegExp(`^${account}/`), ''),
15+
v,
16+
])
17+
.reduce((s, [k, v]) => ({ ...s, [k]: v }), {}),
18+
}),
19+
{},
20+
)
21+
}

src/settings/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './scope.js'
33
export * from './initializeAccount.js'
44
export * from './getAllAccounts.js'
55
export * from './getAllAccountsSettings.js'
6+
export * from './groupByAccount.js'

0 commit comments

Comments
 (0)