Skip to content

Commit

Permalink
Feat(OGCIO): seed users (#126)
Browse files Browse the repository at this point in the history
* feat(cli): added user seeding in local seeder

* feat(cli): seed users code

* feat(cli): seed testing env
  • Loading branch information
SamSalvatico authored Sep 5, 2024
1 parent 8f9d73e commit 8aa017b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/cli/src/commands/database/ogcio/ogcio-seeder-local.json
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@
"signing_key": "webhooks_local_signing_key",
"enabled": true
}
],
"users": [
{
"id": "e2e-user-1",
"username": "e2e_test_user_1",
"primary_email": "e2e_test_1@user.com",
"primary_phone": "+21312666999",
"name": "E2E First User",
"application_id": "4695d8onfb9f3bv18phtq",
"resource_role_ids": ["bb-citizen"]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,5 +440,16 @@
"enabled": true
}
]
}
},
"users": [
{
"id": "e2e-user-1",
"username": "e2e_test_user_1",
"primary_email": "e2e_test_1@user.com",
"primary_phone": "+21312666999",
"name": "E2E First User",
"application_id": "1lvmteh2ao3xrswyq7j3e",
"resource_role_ids": ["bb-citizen"]
}
]
}
11 changes: 11 additions & 0 deletions packages/cli/src/commands/database/ogcio/ogcio-seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type OgcioSeeder = {
sign_in_experiences?: SignInExperienceSeeder[];
resource_permissions?: ResourcePermissionSeeder[];
resource_roles?: ResourceRoleSeeder[];
users?: UserSeeder[];
};

export type OrganizationSeeder = {
Expand Down Expand Up @@ -137,6 +138,16 @@ export type WebhookSeeder = {
enabled: true;
};

export type UserSeeder = {
id: string;
username: string;
primary_email: string;
primary_phone?: string;
name: string;
application_id: string;
resource_role_ids: string[];
};

let inputSeeder: OgcioTenantSeeder | undefined;
export const getTenantSeederData = (): OgcioTenantSeeder => {
if (inputSeeder === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/commands/database/ogcio/ogcio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { createOrganizations } from './organizations.js';
import { seedResourceRbacData } from './resources-rbac.js';
import { seedResources } from './resources.js';
import { seedSignInExperiences } from './sign-in-experiences.js';
import { seedUsers } from './users.js';
import { seedWebhooks } from './webhooks.js';

const createDataForTenant = async (
Expand Down Expand Up @@ -79,6 +80,14 @@ const createDataForTenant = async (
hooks: tenantData.webhooks,
});
}

if (tenantData.users?.length) {
const users = await seedUsers({
transaction,
tenantId,
usersToSeed: tenantData.users,
});
}
};

const transactionMethod = async (transaction: DatabaseTransactionConnection) => {
Expand Down
73 changes: 73 additions & 0 deletions packages/cli/src/commands/database/ogcio/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @silverhand/fp/no-mutating-methods */

import { Users, UsersRoles } from '@logto/schemas';
import { sql, type DatabaseTransactionConnection } from '@silverhand/slonik';

import { type UserSeeder } from './ogcio-seeder.js';
import { createOrUpdateItem } from './queries.js';

export const seedUsers = async (params: {
transaction: DatabaseTransactionConnection;
tenantId: string;
usersToSeed: UserSeeder[];
}) => {
if (params.usersToSeed.length === 0) {
return {};
}

const queries: Array<Promise<void>> = [];

for (const user of params.usersToSeed) {
queries.push(createUser({ ...params, userToSeed: user }));
}

await Promise.all(queries);

return params.usersToSeed;
};

const createUser = async (params: {
transaction: DatabaseTransactionConnection;
tenantId: string;
userToSeed: UserSeeder;
}): Promise<void> => {
await createOrUpdateItem({
transaction: params.transaction,
tenantId: params.tenantId,
toLogFieldName: 'id',
whereClauses: [sql`tenant_id = ${params.tenantId}`, sql`id = ${params.userToSeed.id}`],
toInsert: {
id: params.userToSeed.id,
username: params.userToSeed.username,
primary_email: params.userToSeed.primary_email,
primary_phone: params.userToSeed.primary_phone ?? undefined,
name: params.userToSeed.name,
application_id: params.userToSeed.application_id,
},
tableName: Users.table,
});

const assignRoleQueries = [];
for (const roleId of params.userToSeed.resource_role_ids) {
assignRoleQueries.push(
createOrUpdateItem({
transaction: params.transaction,
tenantId: params.tenantId,
toLogFieldName: 'role_id',
whereClauses: [
sql`tenant_id = ${params.tenantId}`,
sql`user_id = ${params.userToSeed.id}`,
sql`role_id = ${roleId}`,
],
toInsert: {
user_id: params.userToSeed.id,
role_id: roleId,
},
tableName: UsersRoles.table,
})
);
}

await Promise.all(assignRoleQueries);
};

0 comments on commit 8aa017b

Please sign in to comment.