-
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: notification setting for all tasks (#139)
- Loading branch information
Showing
17 changed files
with
486 additions
and
16 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
71 changes: 71 additions & 0 deletions
71
packages/be-gateway/src/routes/project/setting.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,71 @@ | ||
import { | ||
BaseController, | ||
Body, | ||
Controller, | ||
Get, | ||
Put, | ||
Req, | ||
UseMiddleware | ||
} from '../../core' | ||
import { AuthRequest } from '../../types' | ||
import { authMiddleware } from '../../middlewares' | ||
import { ProjectSettingRepository } from '@shared/models' | ||
import BadRequestException from '../../exceptions/BadRequestException' | ||
|
||
@Controller('/project-setting') | ||
@UseMiddleware([authMiddleware]) | ||
class ProjectSetting extends BaseController { | ||
settingRepo: ProjectSettingRepository | ||
constructor() { | ||
super() | ||
this.settingRepo = new ProjectSettingRepository() | ||
} | ||
@Get('/notification') | ||
async getAllNotificationSetting(@Req() req: AuthRequest) { | ||
const { id } = req.authen | ||
const { projectId } = this.req.params as { projectId: string } | ||
try { | ||
|
||
const settings = await this.settingRepo.getMyNotifySettings({ | ||
uid: id, | ||
projectId | ||
}) | ||
|
||
return settings | ||
} catch (error) { | ||
|
||
throw new BadRequestException(error) | ||
} | ||
} | ||
@Put('/notification') | ||
async UpdateNotificationSetting(@Body() body, @Req() req: AuthRequest) { | ||
const { id } = req.authen | ||
const { projectId, taskChanges, remind, overdue } = body as { | ||
projectId: string | ||
taskChanges: boolean | ||
remind: boolean | ||
overdue: boolean | ||
} | ||
|
||
try { | ||
console.log('body', body) | ||
await this.settingRepo.updateOrCreateNotifySetting({ | ||
uid: id, | ||
projectId, | ||
taskChanges: !!taskChanges || false, | ||
remind: !!remind || false, | ||
overdue: !!overdue || false, | ||
createdAt: new Date(), | ||
createdBy: id | ||
}) | ||
|
||
return 1 | ||
} catch (error) { | ||
throw new BadRequestException(error) | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
export default ProjectSetting |
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
66 changes: 66 additions & 0 deletions
66
packages/shared-models/src/lib/project.setting.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,66 @@ | ||
import { ProjectSettingNotification } from '@prisma/client' | ||
import { projectNotifyModel } from './_prisma' | ||
|
||
export class ProjectSettingRepository { | ||
async getMyNotifySettings({ | ||
uid, | ||
projectId | ||
}: { | ||
uid: string | ||
projectId: string | ||
}) { | ||
return projectNotifyModel.findFirst({ | ||
where: { | ||
uid, | ||
projectId | ||
} | ||
}) | ||
} | ||
async getAllNotifySettings(projectId: string) { | ||
const settings = await projectNotifyModel.findMany({ | ||
where: { | ||
projectId, | ||
taskChanges: true | ||
}, | ||
select: { | ||
uid: true | ||
} | ||
}) | ||
|
||
if (settings.length) return settings.map(st => st.uid) | ||
|
||
|
||
return [] | ||
} | ||
|
||
async updateOrCreateNotifySetting( | ||
data: Omit<ProjectSettingNotification, 'id'> | ||
) { | ||
const { uid, projectId } = data | ||
const myNotifySetting = await projectNotifyModel.findFirst({ | ||
where: { | ||
uid, | ||
projectId | ||
} | ||
}) | ||
|
||
if (myNotifySetting) { | ||
return projectNotifyModel.update({ | ||
where: { | ||
id: myNotifySetting.id | ||
}, | ||
data | ||
}) | ||
} else { | ||
return projectNotifyModel.create({ | ||
data | ||
}) | ||
} | ||
} | ||
|
||
async createNotifySetting(data: Omit<ProjectSettingNotification, 'id'>) { | ||
return projectNotifyModel.create({ | ||
data | ||
}) | ||
} | ||
} |
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,45 @@ | ||
import * as Switch from '@radix-ui/react-switch' | ||
import './style.css' | ||
import { useEffect, useState } from 'react' | ||
|
||
interface SwitchProps { | ||
title?: string | ||
checked?: boolean | ||
name?: string | ||
onChange?: (checked: boolean) => void | ||
desc?: string | React.ReactNode | ||
className?: string | ||
disabled?: boolean | ||
} | ||
|
||
export default function SwitchContainer({ | ||
className, | ||
onChange, | ||
checked | ||
}: SwitchProps) { | ||
const [isChecked, setIsChecked] = useState<boolean>(!!checked) | ||
|
||
const handleChange = (checked: boolean) => { | ||
if (onChange) { | ||
onChange(checked) | ||
return | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (isChecked !== checked) { | ||
setIsChecked(!!checked) | ||
// onChange && onChange(!!checked) | ||
} | ||
// eslint-disable-next-line | ||
}, [checked, isChecked]) | ||
|
||
return ( | ||
<Switch.Root | ||
checked={isChecked} | ||
onCheckedChange={handleChange} | ||
className={`switch-root ${className}`}> | ||
<Switch.Thumb className="switch-thumb" /> | ||
</Switch.Root> | ||
) | ||
} |
Oops, something went wrong.