Skip to content

Commit

Permalink
feat(server): add check user exists and fill users by fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 25, 2025
1 parent c19a766 commit 7f65ab2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/backend/server/src/__tests__/models/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,39 @@ test('should paginate users', async t => {
);
});

test('should fill public users', async t => {
const user = await t.context.user.create({
email: '[email protected]',
});
const user2 = await t.context.user.create({
email: '[email protected]',
});
const docs = await t.context.user.fillPublicUsers(
[
{ createdBy: user.id },
{ createdBy: user2.id },
{ createdBy: 'non-existing-user' },
],
'createdBy',
'createdByUser'
);
t.is(docs[0].createdByUser!.id, user.id);
t.is(docs[1].createdByUser!.id, user2.id);
t.is(docs[0].createdByUser!.email, user.email);
t.is(docs[1].createdByUser!.email, user2.email);
t.is(docs[0].createdByUser!.name, user.name);
t.is(docs[1].createdByUser!.name, user2.name);
t.is(docs[2].createdByUser, undefined);
});

test('should check if user exists', async t => {
const user = await t.context.user.create({
email: '[email protected]',
});
t.true(await t.context.user.exists(user.id));
t.false(await t.context.user.exists('non-existing-user'));
});

// #region ConnectedAccount

test('should create, get, update, delete connected account', async t => {
Expand Down
30 changes: 30 additions & 0 deletions packages/backend/server/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export class UserModel extends BaseModel {
});
}

async exists(id: string) {
const count = await this.db.user.count({
where: { id },
});
return count > 0;
}

async getPublicUser(id: string): Promise<PublicUser | null> {
return this.db.user.findUnique({
select: publicUserSelect,
Expand All @@ -75,6 +82,29 @@ export class UserModel extends BaseModel {
});
}

/**
* Fill the public user information to the given datas with the given user field name
*
* @example
* ```ts
* const docsWithUser = await this.models.user.fillPublicUsers(docs, 'createdBy', 'createdByUser');
* ```
*/
async fillPublicUsers<T, F extends string>(
datas: T[],
userIdField: keyof T,
userFieldName: F
): Promise<(T & { [K in F]: PublicUser | undefined })[]> {
const publicUsers = await this.getPublicUsers(
datas.map(d => d[userIdField] as string)
);
const publicUsersMap = new Map(publicUsers.map(pu => [pu.id, pu]));
return datas.map(data => ({
...data,
[userFieldName]: publicUsersMap.get(data[userIdField] as string),
})) as (T & { [K in F]: PublicUser })[];
}

async getUserByEmail(email: string): Promise<User | null> {
const rows = await this.db.$queryRaw<User[]>`
SELECT id, name, email, password, registered, email_verified as emailVerifiedAt, avatar_url as avatarUrl, registered, created_at as createdAt
Expand Down

0 comments on commit 7f65ab2

Please sign in to comment.