-
Notifications
You must be signed in to change notification settings - Fork 0
OUT-2986 | Implement fetching Assembly invoice, contracts and forms counts in API #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0b80db
chore(OUT-2986): add alias for notification-counts feature
rrojan a143991
feat(OUT-2986): make current auth util support dynamic routes
rrojan ca307aa
feat(OUT-2986): add support to getNotifications
rrojan 3a21ffb
fix(OUT-2986): please the typescript overlords
rrojan a49cdce
feat(OUT-2986): add support for getting notification counts for client
rrojan 17ca373
feat(OUT-2986): add route for getting client notification counts
rrojan c546616
refactor(OUT-2986): make sure typescript catches consts
rrojan 282cd9e
refactor(OUT-2986): remove unused import
rrojan 98257a7
feat(OUT-2986): add multi-company support
rrojan e514b7a
refactor(OUT-2986): clean code
rrojan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| import { getAllUserNotificationCounts } from '@notification-counts/lib/notification-counts.controller' | ||
| import { withErrorHandler } from '@/lib/with-error-handler' | ||
|
|
||
| export const GET = withErrorHandler(getAllUserNotificationCounts) |
This file contains hidden or 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 hidden or 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
25 changes: 25 additions & 0 deletions
25
src/features/notification-counts/lib/notification-counts.controller.ts
This file contains hidden or 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,25 @@ | ||
| import { authenticateHeaders } from '@auth/lib/authenticate' | ||
| import NotificationsCountService from '@notification-counts/lib/notification-counts.service' | ||
| import { HttpStatusCode } from 'axios' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import type { APIResponse } from '@/app/types' | ||
| import APIError from '@/errors/api.error' | ||
|
|
||
| export const getAllUserNotificationCounts = async ( | ||
| req: NextRequest, | ||
| { params }: { params: Promise<{ id: string }> }, | ||
| ): Promise<NextResponse<APIResponse>> => { | ||
| const user = authenticateHeaders(req.headers) | ||
| const clientId = (await params).id | ||
|
|
||
| if (!clientId) throw new APIError('Client ID is required', HttpStatusCode.BadRequest) | ||
|
|
||
| const companyId = req.nextUrl.searchParams.get('companyId') | ||
|
|
||
| const notificationCountService = NotificationsCountService.new(user) | ||
| const notificationCounts = await notificationCountService.getNotificationCountsForClient(clientId, companyId) | ||
|
|
||
| return NextResponse.json({ | ||
| data: notificationCounts, | ||
| }) | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/features/notification-counts/lib/notification-counts.service.ts
This file contains hidden or 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,53 @@ | ||
| import AssemblyClient from '@assembly/assembly-client' | ||
| import type { User } from '@auth/lib/user.entity' | ||
| import type { NotificationCountsDto } from '@notification-counts/notification-counts.dto' | ||
| import { NotificationEvent } from '@notification-counts/types' | ||
| import { HttpStatusCode } from 'axios' | ||
| import APIError from '@/errors/api.error' | ||
| import BaseService from '@/lib/core/base.service' | ||
|
|
||
| export default class NotificationsCountService extends BaseService { | ||
| private readonly eventMap = Object.freeze({ | ||
| [NotificationEvent.FORMS]: 'forms', | ||
| [NotificationEvent.INVOICES]: 'invoices', | ||
| [NotificationEvent.CONTRACTS]: 'contracts', | ||
| } as const satisfies Partial<Record<NotificationEvent, keyof NotificationCountsDto>>) | ||
priosshrsth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| constructor( | ||
| readonly user: User, | ||
| readonly assembly: AssemblyClient, | ||
| ) { | ||
| super(user, assembly) | ||
| } | ||
|
|
||
| static new(user: User) { | ||
| const assembly = new AssemblyClient(user.token) | ||
| return new NotificationsCountService(user, assembly) | ||
| } | ||
|
|
||
| async getNotificationCountsForClient( | ||
| recipientClientId: string, | ||
| recipientCompanyId: string | null, | ||
| ): Promise<NotificationCountsDto> { | ||
| const notifications = await this.assembly.getNotifications({ recipientClientId }) | ||
| if (!notifications || !notifications.data) | ||
| throw new APIError('Could not fetch notifications list from assembly', HttpStatusCode.InternalServerError) | ||
|
|
||
| const notificationCounts: NotificationCountsDto = { | ||
| forms: 0, | ||
| invoices: 0, | ||
| contracts: 0, | ||
| tasks: 0, | ||
| messages: 0, | ||
| } | ||
|
|
||
| notifications.data.forEach(({ event, recipientCompanyId: notificationRecipientCompanyId }) => { | ||
| if (recipientCompanyId && notificationRecipientCompanyId !== recipientCompanyId) return | ||
|
|
||
| const key = this.eventMap[event as NotificationEvent] | ||
| if (key) notificationCounts[key]++ | ||
| }) | ||
|
|
||
| return notificationCounts | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/features/notification-counts/notification-counts.dto.ts
This file contains hidden or 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,10 @@ | ||
| import z from 'zod' | ||
|
|
||
| export const NotificationCountsDtoSchema = z.object({ | ||
| forms: z.number(), | ||
| invoices: z.number(), | ||
| contracts: z.number(), | ||
| tasks: z.number(), | ||
| messages: z.number(), | ||
| }) | ||
| export type NotificationCountsDto = z.infer<typeof NotificationCountsDtoSchema> |
This file contains hidden or 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,6 @@ | ||
| export enum NotificationEvent { | ||
| FORMS = 'formResponse.requested', | ||
| INVOICES = 'invoice.requested', | ||
| CONTRACTS = 'contract.requested', | ||
| } | ||
| export type NotificationCounts = Record<NotificationEvent, number> |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -115,3 +115,20 @@ export const InternalUsersResponseSchema = z.object({ | |
| data: z.array(InternalUserResponseSchema), | ||
| }) | ||
| export type InternalUsersResponse = z.infer<typeof InternalUsersResponseSchema> | ||
|
|
||
| // export const NotificationsResponseSchema = z.object({ | ||
| // id: z.string(), // Beware, don't set this to uuid because | ||
| // recipientClientId: z.uuid(), | ||
| // recipientCompanyId: z.uuid().nullish(), | ||
| // }) | ||
| export const NotificationsResponseSchema = z.object({ | ||
| data: z | ||
| .object({ | ||
| id: z.string(), | ||
| recipientClientId: z.uuid(), | ||
| recipientCompanyId: z.uuid().nullish(), | ||
| event: z.string(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also parse this event with |
||
| }) | ||
| .array(), | ||
| }) | ||
| export type NotificationsResponse = z.infer<typeof NotificationsResponseSchema> | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful if we add some docs to what this function actually does.