-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
807 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { params } from '../../../commons/express-joi/params'; | ||
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'; | ||
|
||
export const canGetOrgLogoGuard = [ | ||
params(object({ | ||
orgId: $id, | ||
})), | ||
wrapAsyncMiddleware(async (req: Request, res: Response, next: NextFunction) => { | ||
const { orgId } = req.params; | ||
|
||
// check is admin or owner | ||
const user = getUser(req); | ||
const adminOrOwner = await isAdminOrOwner(user._id, orgId); | ||
if (adminOrOwner) { | ||
return next(); | ||
} | ||
|
||
// check if user has an invite token | ||
if (req.query.invite) { | ||
const count = await Orgs().countDocuments({ | ||
'invites.token': req.query.invite, | ||
}, { | ||
limit: 1, | ||
}); | ||
|
||
if (count !== 0) { | ||
return next(); | ||
} | ||
} | ||
|
||
next(new ForbiddenError('Cannot read org logo')); | ||
}), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Request, Response } from 'express'; | ||
import { wrapAsyncMiddleware } from '../../../commons/utils/wrap-async-middleware'; | ||
import { object } from 'joi'; | ||
import { orgExistsGuard } from '../guards/org-exists-guard'; | ||
import { params } from '../../../commons/express-joi/params'; | ||
import { $id } from '../../../utils/id'; | ||
import { Orgs } from '../org'; | ||
import { getFilePath } from '../../../storage/get-file-path'; | ||
import { NotFoundError } from '../../../commons/errors/not-found-error'; | ||
import { canGetOrgLogoGuard } from '../guards/can-get-org-logo-guard'; | ||
|
||
const validators = [ | ||
params(object({ | ||
orgId: $id, | ||
})), | ||
]; | ||
|
||
async function handler(req: Request, res: Response): Promise<void> { | ||
const { orgId } = req.params; | ||
|
||
const org = await Orgs().findOne({ _id: orgId }); | ||
|
||
if (!org.logo) { | ||
throw new NotFoundError('Org has no logo'); | ||
} | ||
|
||
const filePath = getFilePath(org.logo.id); | ||
|
||
res.header('Content-Type', org.logo.type); | ||
res.download(filePath); | ||
} | ||
|
||
export const getOrgLogo = [ | ||
...orgExistsGuard, | ||
...canGetOrgLogoGuard, | ||
...validators, | ||
wrapAsyncMiddleware(handler), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Request, Response } from 'express'; | ||
import { wrapAsyncMiddleware } from '../../../commons/utils/wrap-async-middleware'; | ||
import { emitEvent } from '../../../events/emit-event'; | ||
import { object } from 'joi'; | ||
import { Orgs } from '../org'; | ||
import { serializeOrg } from '../serialize-org'; | ||
import { canWriteOrgGuard } from '../guards/can-write-org-guard'; | ||
import { orgExistsGuard } from '../guards/org-exists-guard'; | ||
import { EventType } from '../../../events/event-type'; | ||
import { params } from '../../../commons/express-joi/params'; | ||
import { $id } from '../../../utils/id'; | ||
import { deleteFile } from '../../../storage/delete-file'; | ||
import { BadRequestError } from '../../../commons/errors/bad-request-error'; | ||
|
||
const validators = [ | ||
params(object({ | ||
orgId: $id, | ||
})), | ||
]; | ||
|
||
async function handler(req: Request, res: Response): Promise<void> { | ||
const { orgId } = req.params; | ||
|
||
const { logo } = await Orgs().findOne({ | ||
_id: orgId, | ||
}); | ||
|
||
if (!logo) { | ||
throw new BadRequestError('Org has no logo'); | ||
} | ||
|
||
await deleteFile(logo.id); | ||
|
||
await Orgs().updateOne({ | ||
_id: orgId, | ||
}, { | ||
$unset: { | ||
logo: 1, | ||
}, | ||
}); | ||
|
||
const org = await Orgs().findOne({ | ||
_id: orgId, | ||
}); | ||
|
||
emitEvent(EventType.org_logo_removed, { | ||
org, | ||
}); | ||
|
||
res.json(serializeOrg(org)); | ||
} | ||
|
||
export const removeOrgLogo = [ | ||
...orgExistsGuard, | ||
...canWriteOrgGuard, | ||
...validators, | ||
wrapAsyncMiddleware(handler), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Request, Response } from 'express'; | ||
import { wrapAsyncMiddleware } from '../../../commons/utils/wrap-async-middleware'; | ||
import { emitEvent } from '../../../events/emit-event'; | ||
import { object } from 'joi'; | ||
import { Orgs } from '../org'; | ||
import { serializeOrg } from '../serialize-org'; | ||
import { canWriteOrgGuard } from '../guards/can-write-org-guard'; | ||
import { orgExistsGuard } from '../guards/org-exists-guard'; | ||
import { EventType } from '../../../events/event-type'; | ||
import { params } from '../../../commons/express-joi/params'; | ||
import { $id } from '../../../utils/id'; | ||
import { upload } from '../../../upload'; | ||
import { storeFile } from '../../../storage/store-file'; | ||
import { deleteFile } from '../../../storage/delete-file'; | ||
|
||
const validators = [ | ||
params(object({ | ||
orgId: $id, | ||
})), | ||
]; | ||
|
||
async function handler(req: Request, res: Response): Promise<void> { | ||
const { orgId } = req.params; | ||
const { file } = req; | ||
|
||
const { logo: oldLogo } = await Orgs().findOne({ | ||
_id: orgId, | ||
}); | ||
|
||
const storedFile = await storeFile(file); | ||
|
||
await Orgs().updateOne( | ||
{ | ||
_id: orgId, | ||
}, | ||
{ | ||
$set: { | ||
updatedAt: new Date(), | ||
logo: storedFile, | ||
}, | ||
}, | ||
); | ||
|
||
if (oldLogo) { | ||
await deleteFile(oldLogo.id); | ||
} | ||
|
||
const org = await Orgs().findOne({ | ||
_id: orgId, | ||
}); | ||
|
||
emitEvent(EventType.org_logo_set, { | ||
org, | ||
}); | ||
|
||
res.json(serializeOrg(org)); | ||
} | ||
|
||
export const setOrgLogo = [ | ||
...orgExistsGuard, | ||
...canWriteOrgGuard, | ||
upload.single('file'), | ||
...validators, | ||
wrapAsyncMiddleware(handler), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Org } from './org'; | ||
import { env } from '../../env/env'; | ||
|
||
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Request, Response } from 'express'; | ||
import { wrapAsyncMiddleware } from '../../../commons/utils/wrap-async-middleware'; | ||
import { object } from 'joi'; | ||
import { params } from '../../../commons/express-joi/params'; | ||
import { $id } from '../../../utils/id'; | ||
import { getFilePath } from '../../../storage/get-file-path'; | ||
import { NotFoundError } from '../../../commons/errors/not-found-error'; | ||
import { Sites } from '../site'; | ||
import { siteExistsGuard } from '../guards/site-exists-guard'; | ||
import { canAdminSiteGuard } from '../guards/can-admin-site-guard'; | ||
|
||
const validators = [ | ||
params(object({ | ||
siteId: $id, | ||
})), | ||
]; | ||
|
||
async function handler(req: Request, res: Response): Promise<void> { | ||
const { siteId } = req.params; | ||
|
||
const site = await Sites().findOne({ _id: siteId }); | ||
|
||
if (!site.logo) { | ||
throw new NotFoundError('Site has no logo'); | ||
} | ||
|
||
const filePath = getFilePath(site.logo.id); | ||
|
||
res.header('Content-Type', site.logo.type); | ||
res.download(filePath); | ||
} | ||
|
||
export const getSiteLogo = [ | ||
...siteExistsGuard, | ||
...canAdminSiteGuard, | ||
...validators, | ||
wrapAsyncMiddleware(handler), | ||
]; |
Oops, something went wrong.