Skip to content

Commit

Permalink
feat: getById() and delete() requests (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Oct 2, 2023
1 parent dcf0f07 commit f08acb0
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/requests/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export class UserClient {
async getByEmail(email: string): Promise<User | null> {
return getByEmail(this.client, email);
}

async getById(id: number): Promise<User | null> {
return getById(this.client, id);
}

/**
* Soft-delete a user by setting email address to null
*/
async delete(id: number): Promise<boolean> {
return deleteUser(this.client, id);
}
}

export async function createUser(
Expand Down Expand Up @@ -134,3 +145,49 @@ async function getByEmail(
);
return users?.[0] || null;
}

async function getById(
client: GraphQLClient,
id: number,
): Promise<User | null> {
const { user }: { user: User | null } = await client.request(
gql`
query GetUserById($id: Int!) {
user: users_by_pk(id: $id) {
id
firstName: first_name
lastName: last_name
email
isPlatformAdmin: is_platform_admin
teams {
role
team {
name
slug
id
}
}
}
}
`,
{ id },
);
return user;
}

async function deleteUser(client: GraphQLClient, id: number): Promise<boolean> {
const { user }: { user: { id: number | null } } = await client.request(
gql`
mutation SoftDeleteUserById($id: Int!) {
users: update_users_by_pk(
pk_columns: { id: $id }
_set: { email: null }
) {
id
}
}
`,
{ id },
);
return Boolean(user.id);
}

0 comments on commit f08acb0

Please sign in to comment.