-
Notifications
You must be signed in to change notification settings - Fork 1k
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
9 changed files
with
221 additions
and
26 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,66 @@ | ||
import { Request, Response } from 'express'; | ||
import * as Sentry from '@sentry/node'; | ||
import { Types } from 'mongoose'; | ||
import { | ||
Membership, | ||
} from '../../models'; | ||
import Tag, { ITag } from '../../models/tag'; | ||
import { Builder } from "builder-pattern" | ||
import to from 'await-to-js'; | ||
import { BadRequestError, UnauthorizedRequestError } from '../../utils/errors'; | ||
import { MongoError } from 'mongodb'; | ||
import { userHasWorkspaceAccess } from '../../ee/helpers/checkMembershipPermissions'; | ||
|
||
export const createWorkspaceTag = async (req: Request, res: Response) => { | ||
const { workspaceId } = req.params | ||
const { name, slug } = req.body | ||
const sanitizedTagToCreate = Builder<ITag>() | ||
.name(name) | ||
.workspace(new Types.ObjectId(workspaceId)) | ||
.slug(slug) | ||
.user(new Types.ObjectId(req.user._id)) | ||
.build(); | ||
|
||
const [err, createdTag] = await to(Tag.create(sanitizedTagToCreate)) | ||
|
||
if (err) { | ||
if ((err as MongoError).code === 11000) { | ||
throw BadRequestError({ message: "Tags must be unique in a workspace" }) | ||
} | ||
|
||
throw err | ||
} | ||
|
||
res.json(createdTag) | ||
} | ||
|
||
export const deleteWorkspaceTag = async (req: Request, res: Response) => { | ||
const { tagId } = req.params | ||
|
||
const tagFromDB = await Tag.findById(tagId) | ||
if (!tagFromDB) { | ||
throw BadRequestError() | ||
} | ||
|
||
// can only delete if the request user is one that belongs to the same workspace as the tag | ||
const membership = await Membership.findOne({ | ||
user: req.user, | ||
workspace: tagFromDB.workspace | ||
}); | ||
|
||
if (!membership) { | ||
UnauthorizedRequestError({ message: 'Failed to validate membership' }); | ||
} | ||
|
||
await Tag.findByIdAndDelete(tagId) | ||
|
||
res.sendStatus(200) | ||
} | ||
|
||
export const getWorkspaceTags = async (req: Request, res: Response) => { | ||
const { workspaceId } = req.params | ||
const workspaceTags = await Tag.find({ workspace: workspaceId }) | ||
return res.json({ | ||
workspaceTags | ||
}) | ||
} |
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,49 @@ | ||
import { Schema, model, Types } from 'mongoose'; | ||
|
||
export interface ITag { | ||
_id: Types.ObjectId; | ||
name: string; | ||
slug: string; | ||
user: Types.ObjectId; | ||
workspace: Types.ObjectId; | ||
} | ||
|
||
const tagSchema = new Schema<ITag>( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
slug: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
lowercase: true, | ||
validate: [ | ||
function (value: any) { | ||
return value.indexOf(' ') === -1; | ||
}, | ||
'slug cannot contain spaces' | ||
] | ||
}, | ||
user: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'User' | ||
}, | ||
workspace: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Workspace' | ||
}, | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
tagSchema.index({ slug: 1, workspace: 1 }, { unique: true }) | ||
tagSchema.index({ workspace: 1 }) | ||
|
||
const Tag = model<ITag>('Tag', tagSchema); | ||
|
||
export default Tag; |
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
Oops, something went wrong.