Skip to content

Commit

Permalink
feat: show logo to invites and org members
Browse files Browse the repository at this point in the history
  • Loading branch information
gempain committed Jan 13, 2021
1 parent 0074e42 commit c5791fe
Showing 8 changed files with 45 additions and 19 deletions.
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -100,6 +100,7 @@
"passport-google-oauth20": "^2.0.0",
"passport-oauth2": "^1.5.0",
"prom-client": "^12.0.0",
"qs": "^6.9.4",
"queue": "^6.0.1",
"randomcolor": "^0.6.2",
"redis": "^3.0.2",
3 changes: 2 additions & 1 deletion src/entities/invites/serialize-invite.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Org } from '../orgs/org';
import { Invite } from '../orgs/invite';
import { getLogoUrl } from '../../utils/get-logo-url';

export function serializeUserInvite(org: Org, invite: Invite) {
return {
_id: invite._id,
org: {
name: org.name,
color: org.color,
logo: org.logo,
logo: getLogoUrl('orgs', org, { invite: invite.token }),
},
expiresAt: invite.expiresAt,
memberOptions: invite.memberOptions,
10 changes: 5 additions & 5 deletions src/entities/orgs/guards/can-get-org-logo-guard.ts
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@ import { object } from 'joi';
import { NextFunction, Request, Response } from 'express';
import { $id } from '../../../utils/id';
import { Orgs } from '../org';
import { isAdminOrOwner } from '../../../auth/guards/is-admin-or-owner';
import { getUser } from '../../../auth/utils/get-user';
import { wrapAsyncMiddleware } from '../../../commons/utils/wrap-async-middleware';
import { ForbiddenError } from '../../../commons/errors/forbidden-error';
import { isOrgMember } from './is-org-member';

export const canGetOrgLogoGuard = [
params(object({
@@ -15,14 +15,14 @@ export const canGetOrgLogoGuard = [
wrapAsyncMiddleware(async (req: Request, res: Response, next: NextFunction) => {
const { orgId } = req.params;

// check is admin or owner
// check user belongs to org
const user = getUser(req);
const adminOrOwner = await isAdminOrOwner(user._id, orgId);
if (adminOrOwner) {
const orgMember = await isOrgMember(user._id, orgId);
if (orgMember) {
return next();
}

// check if user has an invite token
// check user has an invite token
if (req.query.invite) {
const count = await Orgs().countDocuments({
'invites.token': req.query.invite,
5 changes: 2 additions & 3 deletions src/entities/orgs/serialize-org.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Org } from './org';
import { env } from '../../env/env';
import { getLogoUrl } from '../../utils/get-logo-url';

export function serializeOrg(org: Org) {
return {
_id: org._id,
name: org.name,
color: org.color,
// ?id=... forces cache refresh
logo: org.logo ? `${env.MELI_URL}/api/v1/orgs/${org._id}/logo?id=${org.logo.id}` : undefined,
logo: getLogoUrl('orgs', org),
};
}
5 changes: 2 additions & 3 deletions src/entities/sites/serialize-site.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Site } from './site';
import { getSiteUrl } from './get-site-url';
import { env } from '../../env/env';
import { getLogoUrl } from '../../utils/get-logo-url';

export function serializeSite(site: Site): any {
return {
_id: site._id,
teamId: site.teamId,
color: site.color,
// ?id=... forces cache refresh
logo: site.logo ? `${env.MELI_URL}/api/v1/sites/${site._id}/logo?id=${site.logo.id}` : undefined,
logo: getLogoUrl('sites', site),
createdAt: site.createdAt,
updatedAt: site.updatedAt,
name: site.name,
5 changes: 2 additions & 3 deletions src/entities/teams/serialize-team.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Team } from './team';
import { env } from '../../env/env';
import { getLogoUrl } from '../../utils/get-logo-url';

export function serializeTeam(team: Team) {
return {
@@ -8,7 +8,6 @@ export function serializeTeam(team: Team) {
updatedAt: team.updatedAt,
name: team.name,
color: team.color,
// ?id=... forces cache refresh
logo: team.logo ? `${env.MELI_URL}/api/v1/teams/${team._id}/logo?id=${team.logo.id}` : undefined,
logo: getLogoUrl('teams', team),
};
}
17 changes: 17 additions & 0 deletions src/utils/get-logo-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { env } from '../env/env';
import { StoredFile } from '../storage/store-file';
import qs from 'qs';

interface EntityWithLogo {
_id: string;
logo?: StoredFile;
}

export function getLogoUrl(context: string, entity: EntityWithLogo, extraQueryParams?: { [key: string]: string }): string | undefined {
const query = qs.stringify({
...extraQueryParams,
// force cache refresh in frontend
id: entity.logo.id,
});
return entity.logo ? `${env.MELI_URL}/api/v1/${context}/${entity._id}/logo?${query}` : undefined;
}

0 comments on commit c5791fe

Please sign in to comment.