refactor(Notifications): return modified notification when mutated

updated archiveNotification & unreadNotification mutations to return the
modified notification instead of an overview to improve default caching mechanics
when updating/moving notifications.
This commit is contained in:
Pujit Mehrotra
2024-10-21 17:13:43 -04:00
parent 91bcbc3d6f
commit 9a0c7fe9c8
3 changed files with 15 additions and 14 deletions

View File

@@ -624,7 +624,7 @@ export type Mutation = {
addUser?: Maybe<User>;
archiveAll: NotificationOverview;
/** Marks a notification as archived. */
archiveNotification: NotificationOverview;
archiveNotification: Notification;
archiveNotifications: NotificationOverview;
/** Cancel parity check */
cancelParityCheck?: Maybe<Scalars['JSON']['output']>;
@@ -662,7 +662,7 @@ export type Mutation = {
unarchiveNotifications: NotificationOverview;
unmountArrayDisk?: Maybe<Disk>;
/** Marks a notification as unread. */
unreadNotification: NotificationOverview;
unreadNotification: Notification;
/** Update an existing API key */
updateApikey?: Maybe<ApiKey>;
};
@@ -2347,7 +2347,7 @@ export type MutationResolvers<ContextType = Context, ParentType extends Resolver
addDiskToArray?: Resolver<Maybe<ResolversTypes['Array']>, ParentType, ContextType, Partial<MutationaddDiskToArrayArgs>>;
addUser?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType, RequireFields<MutationaddUserArgs, 'input'>>;
archiveAll?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, Partial<MutationarchiveAllArgs>>;
archiveNotification?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, RequireFields<MutationarchiveNotificationArgs, 'id'>>;
archiveNotification?: Resolver<ResolversTypes['Notification'], ParentType, ContextType, RequireFields<MutationarchiveNotificationArgs, 'id'>>;
archiveNotifications?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, Partial<MutationarchiveNotificationsArgs>>;
cancelParityCheck?: Resolver<Maybe<ResolversTypes['JSON']>, ParentType, ContextType>;
clearArrayDiskStatistics?: Resolver<Maybe<ResolversTypes['JSON']>, ParentType, ContextType, RequireFields<MutationclearArrayDiskStatisticsArgs, 'id'>>;
@@ -2374,7 +2374,7 @@ export type MutationResolvers<ContextType = Context, ParentType extends Resolver
unarchiveAll?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, Partial<MutationunarchiveAllArgs>>;
unarchiveNotifications?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, Partial<MutationunarchiveNotificationsArgs>>;
unmountArrayDisk?: Resolver<Maybe<ResolversTypes['Disk']>, ParentType, ContextType, RequireFields<MutationunmountArrayDiskArgs, 'id'>>;
unreadNotification?: Resolver<ResolversTypes['NotificationOverview'], ParentType, ContextType, RequireFields<MutationunreadNotificationArgs, 'id'>>;
unreadNotification?: Resolver<ResolversTypes['Notification'], ParentType, ContextType, RequireFields<MutationunreadNotificationArgs, 'id'>>;
updateApikey?: Resolver<Maybe<ResolversTypes['ApiKey']>, ParentType, ContextType, RequireFields<MutationupdateApikeyArgs, 'name'>>;
}>;

View File

@@ -18,9 +18,9 @@ type Mutation {
createNotification(input: NotificationData!): Notification!
deleteNotification(id: String!, type: NotificationType!): NotificationOverview!
"""Marks a notification as archived."""
archiveNotification(id: String!): NotificationOverview!
archiveNotification(id: String!): Notification!
"""Marks a notification as unread."""
unreadNotification(id: String!): NotificationOverview!
unreadNotification(id: String!): Notification!
archiveNotifications(ids: [String!]): NotificationOverview!
unarchiveNotifications(ids: [String!]): NotificationOverview!
archiveAll(importance: Importance): NotificationOverview!

View File

@@ -24,6 +24,7 @@ import { v7 as uuidv7 } from 'uuid';
import { CHOKIDAR_USEPOLLING } from '@app/environment';
import { emptyDir } from 'fs-extra';
import { execa } from 'execa';
import { AppError } from '@app/core/errors/app-error';
@Injectable()
export class NotificationsService {
@@ -385,14 +386,14 @@ export class NotificationsService {
};
}
public async archiveNotification({ id }: Pick<Notification, 'id'>): Promise<NotificationOverview> {
public async archiveNotification({ id }: Pick<Notification, 'id'>): Promise<Notification> {
const unreadPath = join(this.paths().UNREAD, id);
// We expect to only archive 'unread' notifications, but it's possible that the notification
// has already been archived or deleted (e.g. retry logic, spike in network latency).
if (!(await fileExists(unreadPath))) {
this.logger.warn(`[archiveNotification] Could not find notification in unreads: ${id}`);
return NotificationsService.overview;
throw new AppError(`Could not find notification in unreads: ${id}`, 404);
}
/**-----------------------
@@ -414,17 +415,17 @@ export class NotificationsService {
await moveToArchive(notification);
return {
...NotificationsService.overview,
archive: snapshot.archive,
...notification,
type: NotificationType.ARCHIVE,
};
}
public async markAsUnread({ id }: Pick<Notification, 'id'>): Promise<NotificationOverview> {
public async markAsUnread({ id }: Pick<Notification, 'id'>): Promise<Notification> {
const archivePath = join(this.paths().ARCHIVE, id);
// the target notification might not be in the archive!
if (!(await fileExists(archivePath))) {
this.logger.warn(`[markAsUnread] Could not find notification in archive: ${id}`);
return NotificationsService.overview;
throw new AppError(`Could not find notification in archive: ${id}`, 404);
}
// we use a snapshot to provide an accurate overview update
@@ -439,8 +440,8 @@ export class NotificationsService {
await moveToUnread(notification);
return {
...NotificationsService.overview,
unread: snapshot.unread,
...notification,
type: NotificationType.UNREAD,
};
}