Skip to content

Commit

Permalink
user notifications fix
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Dec 3, 2023
1 parent ab11360 commit 8eddf15
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
46 changes: 46 additions & 0 deletions apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export function authRoutes(fastify: FastifyInstance) {
}
);

// Generate code and send to email
fastify.post(
"/api/v1/auth/password-reset/code",
async (request: FastifyRequest, reply: FastifyReply) => {
Expand All @@ -121,6 +122,7 @@ export function authRoutes(fastify: FastifyInstance) {
}
);

// Reset users password via code
fastify.post(
"/api/v1/auth/password-reset/password",
async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down Expand Up @@ -510,6 +512,50 @@ export function authRoutes(fastify: FastifyInstance) {
}
);

// Update a users Email notification settings
fastify.put(
"/api/v1/auth/profile/notifcations/emails",
async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];

//checks if token is valid and returns valid token
const token = checkToken(bearer);

if (token) {
let session = await prisma.session.findUnique({
where: {
sessionToken: bearer,
},
});

const {
notify_ticket_created,
notify_ticket_assigned,
notify_ticket_comments,
notify_ticket_status_changed,
} = request.body as any;

let user = await prisma.user.update({
where: { id: session?.userId },
data: {
notify_ticket_created: notify_ticket_created,
notify_ticket_assigned: notify_ticket_assigned,
notify_ticket_comments: notify_ticket_comments,
notify_ticket_status_changed: notify_ticket_status_changed,
},
});

reply.send({
user,
});
} else {
reply.send({
sucess: false,
});
}
}
);

// Logout a user (deletes session)
fastify.get(
"/api/v1/auth/user/:id/logout",
Expand Down
54 changes: 46 additions & 8 deletions apps/client/pages/settings/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ export default function UserNotifications() {
user.ticket_status_changed
);
const [ticket_assigned, setTicket_assigned] = useState(user.ticket_assigned);
const [loading, setLoading] = useState();
const [ticket_comments, setTicket_comments] = useState(user.ticket_comments);

async function updateNotifications() {
await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/profile/config`,
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/profile/notifcations/emails`,
{
method: "POST",
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify({
ticket_creation,
ticket_assigned,
ticket_status,
notify_ticket_created: ticket_creation,
notify_ticket_assigned: ticket_assigned,
notify_ticket_status_changed: ticket_status,
notify_ticket_comments: ticket_comments,
}),
}
);
Expand Down Expand Up @@ -102,7 +103,7 @@ export default function UserNotifications() {
</span>
<Switch
checked={ticket_status}
onChange={() => setTicket_status}
onChange={setTicket_status}
className={classNames(
ticket_status ? "bg-teal-600" : "bg-gray-200",
"relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
Expand Down Expand Up @@ -154,9 +155,46 @@ export default function UserNotifications() {
/>
</Switch>
</Switch.Group>

<Switch.Group
as="div"
className="flex items-center justify-between"
>
<span className="flex-grow flex flex-col">
<Switch.Label
as="span"
className="text-sm font-medium text-gray-900"
passive
>
Ticket Comment
</Switch.Label>
<Switch.Description
as="span"
className="text-sm text-gray-500"
>
Get emailed when a comment is added to your ticket
</Switch.Description>
</span>
<Switch
checked={ticket_comments}
onChange={setTicket_comments}
className={classNames(
ticket_comments ? "bg-teal-600" : "bg-gray-200",
"relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
)}
>
<span
aria-hidden="true"
className={classNames(
ticket_comments ? "translate-x-5" : "translate-x-0",
"pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200"
)}
/>
</Switch>
</Switch.Group>
</div>

<div className="m-4 float-right">
<div className="my-4 float-right">
<button
onClick={() => updateNotifications()}
type="button"
Expand Down

0 comments on commit 8eddf15

Please sign in to comment.