Skip to content

Commit

Permalink
Poké - search by email (#3809)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Feb 20, 2024
1 parent f4024ca commit ea9096f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions front/lib/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type {
ForeignKey,
InferAttributes,
InferCreationAttributes,
NonAttribute,
} from "sequelize";
import { DataTypes, Model } from "sequelize";

import { front_sequelize } from "@app/lib/databases";
import type { Membership } from "@app/lib/models";

export class User extends Model<
InferAttributes<User>,
Expand All @@ -25,6 +27,8 @@ export class User extends Model<
declare imageUrl: string | null;

declare isDustSuperUser: CreationOptional<boolean>;

declare memberships: NonAttribute<Membership[]>;
}
User.init(
{
Expand Down
52 changes: 39 additions & 13 deletions front/pages/api/poke/workspaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { FindOptions, WhereOptions } from "sequelize";
import { Op } from "sequelize";

import { Authenticator, getSession } from "@app/lib/auth";
import { Subscription, Workspace } from "@app/lib/models";
import { Membership, Subscription, User, Workspace } from "@app/lib/models";
import { isEmailValid } from "@app/lib/utils";
import { apiError, withLogging } from "@app/logger/withlogging";

export type GetWorkspacesResponseBody = {
Expand Down Expand Up @@ -109,20 +110,45 @@ async function handler(
}

if (search) {
conditions.push({
[Op.or]: [
{
sId: {
[Op.iLike]: `${search}%`,
},
let isSearchByEmail = false;
if (isEmailValid(search)) {
const user = await User.findOne({
where: {
email: search,
},
{
name: {
[Op.iLike]: `${search}%`,
include: [
{
model: Membership,
attributes: ["workspaceId"],
},
},
],
});
],
});
if (user?.memberships) {
conditions.push({
id: {
[Op.in]: user?.memberships.map((m) => m.workspaceId),
},
});
isSearchByEmail = true;
}
}

if (!isSearchByEmail) {
conditions.push({
[Op.or]: [
{
sId: {
[Op.iLike]: `${search}%`,
},
},
{
name: {
[Op.iLike]: `${search}%`,
},
},
],
});
}
}

const where: FindOptions<Workspace>["where"] = conditions.length
Expand Down

0 comments on commit ea9096f

Please sign in to comment.