mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-02 04:33:40 +00:00
refactor: centralized the iam seeder
This commit is contained in:
20
packages/iam-seed/src/missing-settings.util.ts
Normal file
20
packages/iam-seed/src/missing-settings.util.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Settings of `ownerId` that are not in `existingPairs` (`"<ownerId>:<key>"`)
|
||||
* yet. This is what keeps the IAM baseline seed idempotent: `organization_settings`
|
||||
* and `unit_settings` have no unique index on (owner, key), so a plain re-insert
|
||||
* — or the package seeder's upsert-on-id, which never supplies an id — silently
|
||||
* duplicates every row.
|
||||
*
|
||||
* `value: null` becomes `undefined` so the column default applies on insert.
|
||||
*/
|
||||
export function missingSettings<
|
||||
TSetting extends { key: string; value?: string | null },
|
||||
>(
|
||||
defaults: TSetting[],
|
||||
existingPairs: Set<string>,
|
||||
ownerId: string,
|
||||
): (Omit<TSetting, "value"> & { value?: string })[] {
|
||||
return defaults
|
||||
.filter((setting) => !existingPairs.has(`${ownerId}:${setting.key}`))
|
||||
.map((setting) => ({ ...setting, value: setting.value ?? undefined }));
|
||||
}
|
||||
Reference in New Issue
Block a user