Skip to content

Commit cec00cb

Browse files
committed
search thingy for users
1 parent de3d963 commit cec00cb

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/generated/schema.gql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ input EventsSearchInput {
330330
}
331331

332332
input EventsTicketTemplateSearchInput {
333+
coupon: String
333334
tags: [String!]
334335
}
335336

@@ -1418,6 +1419,7 @@ type UserData {
14181419
input UserSearchValues {
14191420
name: String
14201421
tags: [SearchableUserTags!]
1422+
text: String
14211423
userIds: [String!]
14221424
userName: String
14231425
}

src/generated/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ export type EventsSearchInput = {
332332
};
333333

334334
export type EventsTicketTemplateSearchInput = {
335+
coupon?: InputMaybe<Scalars["String"]["input"]>;
335336
tags?: InputMaybe<Array<Scalars["String"]["input"]>>;
336337
};
337338

@@ -1430,6 +1431,7 @@ export type UserData = {
14301431
export type UserSearchValues = {
14311432
name?: InputMaybe<Scalars["String"]["input"]>;
14321433
tags?: InputMaybe<Array<SearchableUserTags>>;
1434+
text?: InputMaybe<Scalars["String"]["input"]>;
14331435
userIds?: InputMaybe<Array<Scalars["String"]["input"]>>;
14341436
userName?: InputMaybe<Scalars["String"]["input"]>;
14351437
};

src/schema/user/queries.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const UserSearchValues = builder.inputType("UserSearchValues", {
2525
type: "String",
2626
required: false,
2727
}),
28+
text: t.field({
29+
type: "String",
30+
required: false,
31+
}),
2832
}),
2933
});
3034

@@ -81,14 +85,15 @@ builder.queryFields((t) => ({
8185
},
8286
args: createPaginationInputType(t, UserSearchValues),
8387
resolve: async (root, { input }, { DB }) => {
84-
const { name, tags, userIds } = input.search ?? {};
88+
const { name, tags, userIds, text } = input.search ?? {};
8589

8690
const { data, pagination } = await usersFetcher.searchPaginatedUsers({
8791
DB,
8892
search: {
8993
userIds: userIds ?? undefined,
9094
name: name ?? undefined,
9195
tags: tags ?? undefined,
96+
text: text ?? undefined,
9297
},
9398
pagination: input.pagination,
9499
});

src/schema/user/userFetcher.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type UserTicketSearch = {
2121
email?: string;
2222
name?: string;
2323
tags?: AllowedUserTags[];
24+
text?: string;
2425
};
2526

2627
type SortableFields = "createdAt" | "name";
@@ -31,7 +32,7 @@ const getSearchUsersQuery = (
3132
search: UserTicketSearch = {},
3233
sort?: UserFetcherSort,
3334
) => {
34-
const { userIds, userName, name, email, tags } = search;
35+
const { userIds, userName, name, email, tags, text } = search;
3536

3637
const wheres: SQL[] = [];
3738
const query = DB.select().from(usersSchema);
@@ -78,6 +79,23 @@ const getSearchUsersQuery = (
7879
ilike(usersSchema.username, sanitizeForLikeSearch(userName));
7980
}
8081

82+
if (text) {
83+
const spacedText = text.split(" ").map((el) => el.trim());
84+
85+
const orChecks = or(
86+
...spacedText.map((word) =>
87+
ilike(usersSchema.name, sanitizeForLikeSearch(word)),
88+
),
89+
ilike(usersSchema.lastName, sanitizeForLikeSearch(text)),
90+
ilike(usersSchema.username, sanitizeForLikeSearch(text)),
91+
ilike(usersSchema.email, sanitizeForLikeSearch(text)),
92+
);
93+
94+
if (orChecks) {
95+
wheres.push(orChecks);
96+
}
97+
}
98+
8199
const orderBy: SQL<unknown>[] = [];
82100

83101
if (sort) {

0 commit comments

Comments
 (0)