Skip to content

Commit

Permalink
fix: search is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
gempain committed Dec 28, 2020
1 parent a6951b7 commit 9f9eb94
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 26 deletions.
43 changes: 22 additions & 21 deletions src/entities/api/api-scope.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export enum ApiScope {
user_read = 'user.read',
user_disconnect = 'user.disconnect',
user_hook_list = 'user_hook_list',
user_hook_create = 'user_hook_create',
user_hook_read = 'user_hook_read',
user_hook_update = 'user_hook_update',
user_hook_delete = 'user_hook_delete',
user_hook_list = 'user.hook.list',
user_hook_create = 'user.hook.create',
user_hook_read = 'user.hook.read',
user_hook_update = 'user.hook.update',
user_hook_delete = 'user.hook.delete',
org_create = 'org.create',
org_read = 'org.read',
org_list = 'org.list',
org_update = 'org.update',
org_hook_list = 'org_hook_list',
org_hook_create = 'org_hook_create',
org_hook_read = 'org_hook_read',
org_hook_update = 'org_hook_update',
org_hook_delete = 'org_hook_delete',
org_hook_list = 'org.hook.list',
org_hook_create = 'org.hook.create',
org_hook_read = 'org.hook.read',
org_hook_update = 'org.hook.update',
org_hook_delete = 'org.hook.delete',
member_get_current = 'member.get.current',
members_list = 'members.list',
member_delete = 'member.delete',
Expand All @@ -35,14 +35,15 @@ export enum ApiScope {
team_member_delete = 'team.member.delete',
team_sites_list = 'team.sites.list',
team_sites_add = 'team.sites.add',
team_hook_list = 'team_hook_list',
team_hook_create = 'team_hook_create',
team_hook_read = 'team_hook_read',
team_hook_update = 'team_hook_update',
team_hook_delete = 'team_hook_delete',
site_delete = 'site.delete',
team_hook_list = 'team.hook.list',
team_hook_create = 'team.hook.create',
team_hook_read = 'team.hook.read',
team_hook_update = 'team.hook.update',
team_hook_delete = 'team.hook.delete',
sites_list = 'sites.list',
site_read = 'site.read',
site_update = 'site.update',
site_delete = 'site.delete',
site_password_set = 'site.password.set',
site_password_remove = 'site.password.remove',
site_name_validate = 'site.name.validate',
Expand All @@ -57,11 +58,11 @@ export enum ApiScope {
site_branch_password_remove = 'site.branch.password.remove',
site_branch_redirects_read = 'site.branch.redirects.read',
site_branch_redirects_set = 'site.branch.redirects.set',
site_hook_list = 'site_hook_list',
site_hook_create = 'site_hook_create',
site_hook_read = 'site_hook_read',
site_hook_update = 'site_hook_update',
site_hook_delete = 'site_hook_delete',
site_hook_list = 'site.hook.list',
site_hook_create = 'site.hook.create',
site_hook_read = 'site.hook.read',
site_hook_update = 'site.hook.update',
site_hook_delete = 'site.hook.delete',
site_releases_list = 'site.releases.list',
release_upload = 'release.upload',
release_read = 'release.read',
Expand Down
86 changes: 86 additions & 0 deletions src/entities/orgs/handlers/sites/list-sites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Request, Response } from 'express';
import { Site, Sites } from '../../../sites/site';
import { object, string } from 'joi';
import { wrapAsyncMiddleware } from '../../../../commons/utils/wrap-async-middleware';
import { getPagination, pageResponse, pageValidators } from '../../../../utils/getPagination';
import { FilterQuery } from 'mongodb';
import { query } from '../../../../commons/express-joi/query';
import { Teams } from '../../../teams/team';
import { getUser } from '../../../../auth/utils/get-user';
import { params } from '../../../../commons/express-joi/params';
import { $id } from '../../../../utils/id';
import { Members } from '../../../members/member';
import { isOrgMemberGuard } from '../../guards/is-org-member-guard';
import { orgExistsGuard } from '../../guards/org-exists-guard';
import { isAdminOrOwner } from '../../../../auth/guards/is-admin-or-owner';

const validators = [
...pageValidators,
params(object({
orgId: $id,
})),
query({
search: {
$schema: string().optional().empty('').min(3)
.max(255),
},
}),
];

async function handler(req: Request, res: Response): Promise<void> {
const { orgId } = req.params;
const { search } = req.query;
const pagination = getPagination(req);

// find user teams in this org
const user = getUser(req);
const adminOrOwner = await isAdminOrOwner(user._id, orgId);
const member = await Members().findOne({
orgId,
userId: user._id,
});

const teams = await Teams()
.find(adminOrOwner ? {} : {
members: member._id,
})
.project({ _id: 1 })
.toArray();

const dbQuery: FilterQuery<Site> = {
teamId: {
$in: teams.map(({ _id }) => _id),
},
...(
search
? {
$text: {
$search: search,
},
}
: undefined as any
),
};

const count = await Sites()
.find(dbQuery)
.count();

const sites = await Sites()
.find(dbQuery)
.sort({
updatedAt: -1,
})
.skip(pagination.offset)
.limit(pagination.size)
.toArray();

res.json(pageResponse(sites, count));
}

export const listSites = [
...orgExistsGuard,
...isOrgMemberGuard,
...validators,
wrapAsyncMiddleware(handler),
];
12 changes: 12 additions & 0 deletions src/entities/orgs/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { deleteInvite } from './handlers/invites/delete-invite';
import { createOrg } from './handlers/create-org';
import { apiEndpoint } from '../api/api-endpoint';
import { ApiScope } from '../api/api-scope';
import { listSites } from './handlers/sites/list-sites';

const router = Router();

Expand Down Expand Up @@ -123,4 +124,15 @@ apiEndpoint({
router,
});

// sites
apiEndpoint({
name: 'list sites',
method: 'get',
path: '/api/v1/orgs/:orgId/sites',
handler: listSites,
auth: true,
apiScope: ApiScope.sites_list,
router,
});

export default router;
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function handler(req: Request, res: Response): Promise<void> {
res.json(pageResponse(sites, count));
}

export const listSites = [
export const listTeamSites = [
...teamExistsGuard,
...canReadTeamGuard,
...validators,
Expand Down
4 changes: 2 additions & 2 deletions src/entities/teams/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { deleteTeam } from './handlers/delete-team';
import { getTeam } from './handlers/get-team';
import { addMember } from './handlers/members/add-member';
import { deleteMember } from './handlers/members/delete-member';
import { listSites } from './handlers/sites/list-sites';
import { listTeamSites } from './handlers/sites/list-team-sites';
import { listMembers } from './handlers/members/list-members';
import { addSite } from './handlers/sites/add-site';
import { apiEndpoint } from '../api/api-endpoint';
Expand Down Expand Up @@ -46,7 +46,7 @@ apiEndpoint({
name: 'list sites',
method: 'get',
path: '/api/v1/teams/:teamId/sites',
handler: listSites,
handler: listTeamSites,
auth: true,
apiScope: ApiScope.team_sites_list,
router,
Expand Down
5 changes: 3 additions & 2 deletions src/utils/getPagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export const pageValidators = [
query({
size: {
transform: stringToInt(),
$schema: number().default(10).min(0).max(100),
$schema: number().optional().default(10).min(0)
.max(100),
},
page: {
transform: stringToInt(),
$schema: number().default(0).min(0),
$schema: number().optional().default(0).min(0),
},
}),
];
Expand Down

0 comments on commit 9f9eb94

Please sign in to comment.