-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add checklist to each task (#160)
- Loading branch information
Showing
31 changed files
with
808 additions
and
40 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
40 changes: 40 additions & 0 deletions
40
packages/be-gateway/src/routes/task/checklist.controller.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,40 @@ | ||
import { BaseController, UseMiddleware, Controller, Get, Post, Req, Delete, Put, Body, Param } from "../../core"; | ||
import { authMiddleware, beProjectMemberMiddleware } from "../../middlewares"; | ||
import { TaskChecklist } from "@prisma/client"; | ||
import TaskChecklistService from "../../services/task/checklist.service"; | ||
|
||
@Controller('/project/task/checklist') | ||
@UseMiddleware([authMiddleware, beProjectMemberMiddleware]) | ||
export default class TaskChecklistController extends BaseController { | ||
private checklistService: TaskChecklistService | ||
|
||
constructor() { | ||
super() | ||
this.checklistService = new TaskChecklistService() | ||
} | ||
@Get('/:taskId') | ||
async getChecklistByTaskId(@Param() params: { taskId: string }) { | ||
console.log(1) | ||
const { taskId } = params | ||
const results = await this.checklistService.get(taskId) | ||
return results | ||
} | ||
|
||
@Put('') | ||
async updateChecklist(@Body() body: TaskChecklist) { | ||
const ret = await this.checklistService.update(body) | ||
return ret | ||
} | ||
|
||
@Post('') | ||
async createChecklist(@Body() body: TaskChecklist) { | ||
const result = await this.checklistService.create(body) | ||
return result | ||
} | ||
|
||
@Delete('/:checklistId') | ||
async deleteChecklist(@Param() params: { checklistId: string }) { | ||
const result = await this.checklistService.delete(params.checklistId) | ||
return result | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
packages/be-gateway/src/services/task/checklist.service.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,122 @@ | ||
import { Task, TaskChecklist } from "@prisma/client" | ||
import BadRequestException from "../../exceptions/BadRequestException" | ||
import { TaskChecklistRepository, mdTaskGetOne, mdTaskUpdate } from "@shared/models" | ||
import { CKEY, findNDelCaches } from "../../lib/redis" | ||
|
||
export default class TaskChecklistService { | ||
private checklistRepo: TaskChecklistRepository | ||
constructor() { | ||
this.checklistRepo = new TaskChecklistRepository() | ||
} | ||
|
||
async get(taskId: string) { | ||
const results = await this.checklistRepo.getAllByTaskId(taskId) | ||
return results | ||
} | ||
|
||
async delete(checklistId: string) { | ||
|
||
if (!checklistId) { | ||
throw new BadRequestException() | ||
} | ||
|
||
const result = await this.checklistRepo.deleteById(checklistId) | ||
await this._updateTaskChecklistCounter(result.taskId) | ||
|
||
console.log(result) | ||
|
||
return result | ||
|
||
} | ||
|
||
async create(body: TaskChecklist) { | ||
const { title, order, taskId } = body | ||
|
||
const result = await this.checklistRepo.create({ | ||
title, | ||
order: 1, | ||
taskId, | ||
done: false, | ||
doneAt: null | ||
}) | ||
|
||
await this._updateTaskChecklistCounter(taskId) | ||
|
||
return result | ||
|
||
} | ||
|
||
async update(data: TaskChecklist) { | ||
const { title, done, order, id } = data | ||
|
||
|
||
if (!id) { | ||
throw new BadRequestException() | ||
} | ||
|
||
const checklistData = await this.checklistRepo.getById(id) | ||
let changed = false | ||
|
||
if (title !== checklistData.title) { | ||
checklistData.title = title | ||
changed = true | ||
} | ||
|
||
if (done !== checklistData.done) { | ||
checklistData.done = done | ||
changed = true | ||
} | ||
|
||
if (order !== checklistData.order) { | ||
checklistData.order = order | ||
|
||
if (order) { | ||
checklistData.doneAt = new Date() | ||
} | ||
|
||
changed = true | ||
} | ||
|
||
if (changed) { | ||
const { id, ...restData } = checklistData | ||
await this.checklistRepo.updateById(id, restData) | ||
await this._updateTaskChecklistCounter(checklistData.taskId) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
|
||
} | ||
|
||
private async _updateTaskChecklistCounter(taskId) { | ||
const promises = [mdTaskGetOne(taskId), this.checklistRepo.getAllByTaskId(taskId)] | ||
|
||
const data = await Promise.all(promises) | ||
const taskData = data[0] as Task | ||
const checklist = data[1] as TaskChecklist[] | ||
|
||
|
||
if (!checklist || !checklist.length) return | ||
|
||
const [todo, done] = checklist.reduce((total, c) => { | ||
if (c.done) { | ||
total[1] += 1 | ||
} else { | ||
total[0] += 1 | ||
} | ||
|
||
return total | ||
}, [0, 0]) | ||
|
||
|
||
taskData.checklistTodos = todo | ||
taskData.checklistDone = done | ||
|
||
await mdTaskUpdate(taskData) | ||
|
||
const key = [CKEY.TASK_QUERY, taskData.projectId] | ||
await findNDelCaches(key) | ||
|
||
} | ||
|
||
} |
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
60 changes: 60 additions & 0 deletions
60
packages/shared-models/src/lib/task.checklist.repository.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,60 @@ | ||
import { TaskChecklist } from "@prisma/client" | ||
import { taskChecklistModel } from "./_prisma" | ||
|
||
export class TaskChecklistRepository { | ||
constructor(private model = taskChecklistModel) { | ||
|
||
} | ||
|
||
async getById(cid: string) { | ||
return this.model.findFirst({ | ||
where: { | ||
id: cid | ||
} | ||
}) | ||
} | ||
|
||
async getAllByTaskId(taskId: string) { | ||
return this.model.findMany({ | ||
where: { | ||
taskId | ||
}, | ||
orderBy: { | ||
order: 'asc' | ||
} | ||
|
||
}) | ||
} | ||
|
||
async create(data: Omit<TaskChecklist, 'id'>) { | ||
|
||
return this.model.create({ | ||
data | ||
}) | ||
} | ||
|
||
async updateById(id: string, data: Partial<Omit<TaskChecklist, 'id'>>) { | ||
return this.model.update({ | ||
where: { | ||
id | ||
}, | ||
data | ||
}) | ||
} | ||
|
||
async deleteById(id: string) { | ||
return this.model.delete({ | ||
where: { id } | ||
}) | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
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
Oops, something went wrong.