-
Notifications
You must be signed in to change notification settings - Fork 143
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
36 changed files
with
1,683 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { Request, Response } from "express"; | ||
import { getIO } from "../libs/socket"; | ||
|
||
import AppError from "../errors/AppError"; | ||
|
||
import CreateService from "../services/TagServices/CreateService"; | ||
import ListService from "../services/TagServices/ListService"; | ||
import UpdateService from "../services/TagServices/UpdateService"; | ||
import ShowService from "../services/TagServices/ShowService"; | ||
import DeleteService from "../services/TagServices/DeleteService"; | ||
import SimpleListService from "../services/TagServices/SimpleListService"; | ||
import SyncTagService from "../services/TagServices/SyncTagsService"; | ||
import DeleteAllService from "../services/TagServices/DeleteAllService"; | ||
|
||
type IndexQuery = { | ||
searchParam?: string; | ||
pageNumber?: string | number; | ||
}; | ||
|
||
export const index = async (req: Request, res: Response): Promise<Response> => { | ||
const { pageNumber, searchParam } = req.query as IndexQuery; | ||
|
||
const { tags, count, hasMore } = await ListService({ | ||
searchParam, | ||
pageNumber | ||
}); | ||
|
||
return res.json({ tags, count, hasMore }); | ||
}; | ||
|
||
export const store = async (req: Request, res: Response): Promise<Response> => { | ||
const { name, color } = req.body; | ||
|
||
const tag = await CreateService({ | ||
name, | ||
color | ||
}); | ||
|
||
const io = getIO(); | ||
io.emit("tag", { | ||
action: "create", | ||
tag | ||
}); | ||
|
||
return res.status(200).json(tag); | ||
}; | ||
|
||
export const show = async (req: Request, res: Response): Promise<Response> => { | ||
const { tagId } = req.params; | ||
|
||
const tag = await ShowService(tagId); | ||
|
||
return res.status(200).json(tag); | ||
}; | ||
|
||
export const update = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response> => { | ||
if (req.user.profile !== "admin") { | ||
throw new AppError("ERR_NO_PERMISSION", 403); | ||
} | ||
|
||
const { tagId } = req.params; | ||
const tagData = req.body; | ||
|
||
const tag = await UpdateService({ tagData, id: tagId }); | ||
|
||
const io = getIO(); | ||
io.emit("tag", { | ||
action: "update", | ||
tag | ||
}); | ||
|
||
return res.status(200).json(tag); | ||
}; | ||
|
||
export const remove = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response> => { | ||
const { tagId } = req.params; | ||
|
||
await DeleteService(tagId); | ||
|
||
const io = getIO(); | ||
io.emit("tag", { | ||
action: "delete", | ||
tagId | ||
}); | ||
|
||
return res.status(200).json({ message: "Tag deleted" }); | ||
}; | ||
|
||
export const removeAll = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<Response> => { | ||
const { tagId } = req.params; | ||
|
||
await DeleteAllService(); | ||
|
||
return res.send(); | ||
}; | ||
|
||
export const list = async (req: Request, res: Response): Promise<Response> => { | ||
const { searchParam } = req.query as IndexQuery; | ||
|
||
const tags = await SimpleListService({ searchParam }); | ||
|
||
return res.json(tags); | ||
}; | ||
|
||
export const syncTags = async (req: Request, res: Response): Promise<any> => { | ||
const data = req.body; | ||
|
||
try { | ||
if (data) { | ||
const tags = await SyncTagService(data); | ||
|
||
return res.json(tags); | ||
} | ||
} catch (err) { | ||
throw new AppError("ERR_SYNC_TAGS", 500); | ||
} | ||
}; |
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
34 changes: 34 additions & 0 deletions
34
backend/src/database/migrations/20220906150400-create-tags.ts
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,34 @@ | ||
import { QueryInterface, DataTypes } from "sequelize"; | ||
|
||
module.exports = { | ||
up: (queryInterface: QueryInterface) => { | ||
return queryInterface.createTable("Tags", { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
allowNull: false | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
color: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
createdAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false | ||
}, | ||
updatedAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false | ||
} | ||
}); | ||
}, | ||
|
||
down: (queryInterface: QueryInterface) => { | ||
return queryInterface.dropTable("Tags"); | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
backend/src/database/migrations/20220906150600-create-associate-contacttags.ts
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,34 @@ | ||
import { QueryInterface, DataTypes } from "sequelize"; | ||
|
||
module.exports = { | ||
up: (queryInterface: QueryInterface) => { | ||
return queryInterface.createTable("ContactTags", { | ||
contactId: { | ||
type: DataTypes.INTEGER, | ||
references: { model: "Contacts", key: "id" }, | ||
onUpdate: "CASCADE", | ||
onDelete: "CASCADE", | ||
allowNull: false | ||
}, | ||
tagId: { | ||
type: DataTypes.INTEGER, | ||
references: { model: "Tags", key: "id" }, | ||
onUpdate: "CASCADE", | ||
onDelete: "CASCADE", | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false | ||
}, | ||
updatedAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false | ||
} | ||
}); | ||
}, | ||
|
||
down: (queryInterface: QueryInterface) => { | ||
return queryInterface.dropTable("ContactTags"); | ||
} | ||
}; |
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,38 @@ | ||
import { | ||
Table, | ||
Column, | ||
CreatedAt, | ||
UpdatedAt, | ||
Model, | ||
ForeignKey, | ||
BelongsTo | ||
} from "sequelize-typescript"; | ||
import Tag from "./Tag"; | ||
import Contact from "./Contact"; | ||
|
||
@Table({ | ||
tableName: "ContactTags" | ||
}) | ||
class ContactTag extends Model<ContactTag> { | ||
@ForeignKey(() => Contact) | ||
@Column | ||
contactId: number; | ||
|
||
@ForeignKey(() => Tag) | ||
@Column | ||
tagId: number; | ||
|
||
@CreatedAt | ||
createdAt: Date; | ||
|
||
@UpdatedAt | ||
updatedAt: Date; | ||
|
||
@BelongsTo(() => Contact) | ||
contact: Contact; | ||
|
||
@BelongsTo(() => Tag) | ||
tag: Tag; | ||
} | ||
|
||
export default ContactTag; |
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,37 @@ | ||
import { | ||
Table, | ||
Column, | ||
CreatedAt, | ||
UpdatedAt, | ||
Model, | ||
PrimaryKey, | ||
AutoIncrement, | ||
BelongsToMany | ||
} from "sequelize-typescript"; | ||
import Contact from "./Contact"; | ||
import ContactTag from "./ContactTag"; | ||
|
||
@Table | ||
class Tag extends Model<Tag> { | ||
@PrimaryKey | ||
@AutoIncrement | ||
@Column | ||
id: number; | ||
|
||
@Column | ||
name: string; | ||
|
||
@Column | ||
color: string; | ||
|
||
@BelongsToMany(() => Contact, () => ContactTag) | ||
contacts: Contact[]; | ||
|
||
@CreatedAt | ||
createdAt: Date; | ||
|
||
@UpdatedAt | ||
updatedAt: Date; | ||
} | ||
|
||
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
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,17 @@ | ||
import express from "express"; | ||
import isAuth from "../middleware/isAuth"; | ||
|
||
import * as TagController from "../controllers/TagController"; | ||
|
||
const tagRoutes = express.Router(); | ||
|
||
tagRoutes.get("/tags/list", isAuth, TagController.list); | ||
tagRoutes.get("/tags", isAuth, TagController.index); | ||
tagRoutes.post("/tags", isAuth, TagController.store); | ||
tagRoutes.put("/tags/:tagId", isAuth, TagController.update); | ||
tagRoutes.get("/tags/:tagId", isAuth, TagController.show); | ||
tagRoutes.delete("/tags/:tagId", isAuth, TagController.remove); | ||
tagRoutes.delete("/tags", isAuth, TagController.removeAll); | ||
tagRoutes.post("/tags/sync", isAuth, TagController.syncTags); | ||
|
||
export default tagRoutes; |
Oops, something went wrong.