Skip to content

Commit

Permalink
feat(user): add User in invitation list method (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
dipendraupreti committed Aug 2, 2023
1 parent bc78f3e commit 44bd832
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
38 changes: 36 additions & 2 deletions packages/user/src/model/invitations/sqlFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { DefaultSqlFactory } from "@dzangolab/fastify-slonik";
import {
DefaultSqlFactory,
createTableIdentifier,
createFilterFragment,
createSortFragment,
createLimitFragment,
createTableFragment,
} from "@dzangolab/fastify-slonik";
import { sql } from "slonik";

import type { SqlFactory } from "@dzangolab/fastify-slonik";
import type {
SqlFactory,
FilterInput,
SortInput,
} from "@dzangolab/fastify-slonik";
import type { QueryResultRow, QuerySqlToken } from "slonik";

/* eslint-disable brace-style */
Expand All @@ -26,6 +37,29 @@ class InvitationSqlFactory<
WHERE token = ${token};
`;
};

getListSql = (
limit: number,
offset?: number,
filters?: FilterInput,
sort?: SortInput[]
): QuerySqlToken => {
const tableIdentifier = createTableIdentifier(this.table, this.schema);

const usersTable = createTableFragment(
this.config.user.table?.name || "users",
this.schema
);

return sql.type(this.validationSchema)`
SELECT ${this.getTableFragment()}.*, ROW_TO_JSON("user") as "invited_by"
FROM ${this.getTableFragment()}
join ${usersTable} "user" on ${this.getTableFragment()}."invited_by_id" = "user"."id"
${createFilterFragment(filters, tableIdentifier)}
${createSortFragment(tableIdentifier, this.getSortInput(sort))}
${createLimitFragment(limit, offset)};
`;
};
}

export default InvitationSqlFactory;
5 changes: 5 additions & 0 deletions packages/user/src/types/invitation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { User } from "./index";

interface Invitation {
id: number;
acceptedAt?: number;
appId?: number;
email: string;
expiresAt: number;
invitedBy?: User;
invitedById: string;
payload?: Record<string, unknown>;
revokedAt?: number;
Expand All @@ -18,6 +21,7 @@ type InvitationCreateInput = Omit<
| "id"
| "acceptedAt"
| "expiresAt"
| "invitedBy"
| "payload"
| "revokedAt"
| "token"
Expand All @@ -36,6 +40,7 @@ type InvitationUpdateInput = Partial<
| "appId"
| "email"
| "expiresAt"
| "invitedBy"
| "invitedById"
| "payload"
| "revokedAt"
Expand Down

0 comments on commit 44bd832

Please sign in to comment.