Notifications interface (#5354)
Co-authored-by: Apoorv Mishra <apoorvmishra101092@gmail.com>
This commit is contained in:
187
server/commands/notificationUpdater.test.ts
Normal file
187
server/commands/notificationUpdater.test.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import { Event } from "@server/models";
|
||||
import {
|
||||
buildUser,
|
||||
buildNotification,
|
||||
buildDocument,
|
||||
buildCollection,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import notificationUpdater from "./notificationUpdater";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("notificationUpdater", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should mark the notification as viewed", async () => {
|
||||
const user = await buildUser();
|
||||
const actor = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
collectionId: collection.id,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const notification = await buildNotification({
|
||||
actorId: actor.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
notificationUpdater({
|
||||
notification,
|
||||
viewedAt: new Date(),
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(notification.viewedAt).not.toBe(null);
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
expect(event!.name).toEqual("notifications.update");
|
||||
expect(event!.modelId).toEqual(notification.id);
|
||||
});
|
||||
|
||||
it("should mark the notification as unseen", async () => {
|
||||
const user = await buildUser();
|
||||
const actor = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
collectionId: collection.id,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const notification = await buildNotification({
|
||||
actorId: actor.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
viewedAt: new Date(),
|
||||
});
|
||||
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
expect(notification.viewedAt).not.toBe(null);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
notificationUpdater({
|
||||
notification,
|
||||
viewedAt: null,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
expect(event!.name).toEqual("notifications.update");
|
||||
expect(event!.modelId).toEqual(notification.id);
|
||||
});
|
||||
|
||||
it("should archive the notification", async () => {
|
||||
const user = await buildUser();
|
||||
const actor = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
collectionId: collection.id,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const notification = await buildNotification({
|
||||
actorId: actor.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
notificationUpdater({
|
||||
notification,
|
||||
archivedAt: new Date(),
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
expect(notification.archivedAt).not.toBe(null);
|
||||
expect(event!.name).toEqual("notifications.update");
|
||||
expect(event!.modelId).toEqual(notification.id);
|
||||
});
|
||||
|
||||
it("should unarchive the notification", async () => {
|
||||
const user = await buildUser();
|
||||
const actor = await buildUser({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
collectionId: collection.id,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const notification = await buildNotification({
|
||||
actorId: actor.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
archivedAt: new Date(),
|
||||
});
|
||||
|
||||
expect(notification.archivedAt).not.toBe(null);
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
notificationUpdater({
|
||||
notification,
|
||||
archivedAt: null,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(notification.viewedAt).toBe(null);
|
||||
expect(notification.archivedAt).toBeNull();
|
||||
expect(event!.name).toEqual("notifications.update");
|
||||
expect(event!.modelId).toEqual(notification.id);
|
||||
});
|
||||
});
|
||||
56
server/commands/notificationUpdater.ts
Normal file
56
server/commands/notificationUpdater.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { isUndefined } from "lodash";
|
||||
import { Transaction } from "sequelize";
|
||||
import { Event, Notification } from "@server/models";
|
||||
|
||||
type Props = {
|
||||
/** Notification to be updated */
|
||||
notification: Notification;
|
||||
/** Time at which notification was viewed */
|
||||
viewedAt?: Date | null;
|
||||
/** Time at which notification was archived */
|
||||
archivedAt?: Date | null;
|
||||
/** The IP address of the user updating the notification */
|
||||
ip: string;
|
||||
/** The database transaction to run within */
|
||||
transaction: Transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
* This command updates notification properties.
|
||||
*
|
||||
* @param Props The properties of the notification to update
|
||||
* @returns Notification The updated notification
|
||||
*/
|
||||
export default async function notificationUpdater({
|
||||
notification,
|
||||
viewedAt,
|
||||
archivedAt,
|
||||
ip,
|
||||
transaction,
|
||||
}: Props): Promise<Notification> {
|
||||
if (!isUndefined(viewedAt)) {
|
||||
notification.viewedAt = viewedAt;
|
||||
}
|
||||
if (!isUndefined(archivedAt)) {
|
||||
notification.archivedAt = archivedAt;
|
||||
}
|
||||
const changed = notification.changed();
|
||||
if (changed) {
|
||||
await notification.save({ transaction });
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "notifications.update",
|
||||
userId: notification.userId,
|
||||
modelId: notification.id,
|
||||
teamId: notification.teamId,
|
||||
documentId: notification.documentId,
|
||||
actorId: notification.actorId,
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
return notification;
|
||||
}
|
||||
Reference in New Issue
Block a user