Skip to content

Commit

Permalink
readNotification, orderNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
ceddybi committed Mar 31, 2024
1 parent 9c0b97a commit 0fb42c7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 43 deletions.
31 changes: 23 additions & 8 deletions src/notification/notification.methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Notification, NotificationModel } from "./notification.model";
import { compact, flattenDeep, isEmpty } from "lodash";

import { awaitTo } from "couchset/dist/utils";
import { createUpdate } from "couchset";
import { isEmpty } from "lodash";
import { updateBadgeCount } from "../badge/Badge.methods";

export const createNotification = async (notification: Notification, silent = false): Promise<Notification | null> => {
try {
Expand All @@ -27,25 +28,39 @@ export const createNotification = async (notification: Notification, silent = fa
}
}

export const updateReadStatus = async (query: any): Promise<Notification[] | null> => {
interface UpdateReadStatus {
limit?: number;
read?: boolean;
};

export const updateReadStatus = async (query: any, opt?: UpdateReadStatus): Promise<Notification[] | null> => {
try {
const notifications = await NotificationModel.pagination({
where: query,
limit: 1000, // TODO pagination
limit: opt?.limit || 1000, // TODO pagination
});

if (isEmpty(notifications)) {
throw new Error("error getting notifications");
}

const updatedNotifications = await Promise.all(notifications.map(async (notification) => {
return await NotificationModel.updateById<Notification>(notification.id, {
const updateNotificationsNBadge = await Promise.all(notifications.map(async (notification) => {
const [errorUpdate, updatedNotifications] = await awaitTo(NotificationModel.updateById<Notification>(notification.id, {
...notification,
read: true,
});
read: opt?.read || true,
}));

if (errorUpdate || !updatedNotifications) {
console.log("error updateNotificationsNBadge",errorUpdate)
return null;
}

await updateBadgeCount(notification.owner as string, Notification.name, -1)

return updatedNotifications;
}));

return updatedNotifications;
return compact(updateNotificationsNBadge);

} catch (error) {
console.error("error updating notification", error);
Expand Down
61 changes: 61 additions & 0 deletions src/notification/notification.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import NotificationModel, { NotificationModelName, Notification } from './notifi
import { ContextType, isAuth } from '@roadmanjs/auth';
import { CouchbaseConnection, getPagination } from 'couchset';
import { awaitTo } from 'couchset/dist/utils';
import { updateReadStatus } from './notification.methods';
import { isEmpty } from 'lodash';

const NotificationPagination = getPagination(Notification);
@Resolver()
Expand Down Expand Up @@ -94,6 +96,65 @@ export class NotificationResolver {
return { items: [], hasNext: false, params: copyParams };
}
}

@Query(() => [Notification])
@UseMiddleware(isAuth)
async readNotifications(
@Ctx() ctx: ContextType,
@Arg('id', () => String, { nullable: true }) id?: string,
@Arg('before', () => Date, { nullable: true }) before?: Date,
@Arg('limit', () => Number, { nullable: true }) limit: number = 1000
): Promise<Notification[] | null> {
try {
const owner = _get(ctx, 'payload.userId', '');

let updatedNotifications: Notification[] | null = [];

if (id) {
updatedNotifications = await updateReadStatus({ id });

} else {
if (!owner || !before) {
throw new Error("owner and before date are required");
}

updatedNotifications = await updateReadStatus({
owner,
before: { $lte: before },
}, { limit });

}
return updatedNotifications;
}
catch (error) {
log("error reading notification", error);
return null;
}

}

@Query(() => Notification, { nullable: true })
@UseMiddleware(isAuth)
async getNotification(
@Arg('id', () => String, { nullable: false }) id: string,
): Promise<Notification | null> {
try {
if (!id || isEmpty(id)) {
throw new Error("id is required");
}

const [error, notification] = await awaitTo(NotificationModel.findById(id));
if (error) {
throw error;
}
return notification;
}
catch (error) {
log("error reading notification", error);
return null;
}

}
}

export default NotificationResolver;
34 changes: 4 additions & 30 deletions src/order/order.notifications.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
// orderCreated

import { Notification, createNotification } from "../notification";
import { OrderModelName, OrderType } from "./order.model";

import { OrderType } from "./order.model";
import { awaitTo } from "couchset/dist/utils";
import { log } from "roadman";
import { updateBadgeCount } from "../badge/Badge.methods";

// order / accept / rejected / completed / canceled / dispute / disputeResolved / refund / refundResolved .e.......

export const orderCreatedNotification = async (order: OrderType) => {
export const orderNotification = async (order: OrderType, message: string) => {
const {id, owner } = order;
const notification: Notification = {
source: OrderType.name,
source: OrderModelName,
sourceId: id,
message: "New order has been created",
owner: owner as any,
read: false,
};
const [error, notificationCreated] = await awaitTo(createNotification(notification));

const [errorBadge, updatedBadge] = await awaitTo(updateBadgeCount(owner as string, Notification.name, 1));

if (errorBadge) {
log("error updating badge", errorBadge);
} else {
log("updatedBadge", updatedBadge);
}

if (error) {
console.error("error creating notification", error);
return null;
}
return notificationCreated;
}

export const orderCancelledNotification = async (order: OrderType) => {
const {id, owner } = order;
const notification: Notification = {
source: OrderType.name,
sourceId: id,
message: "Order has been cancelled",
message,
owner: owner as any,
read: false,
};
Expand Down
8 changes: 3 additions & 5 deletions src/order/order.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { WalletModel, updateWallet } from "@roadmanjs/wallet";
import OrderRatingModel from "./orderRating.model";
import { encryptCode, getVerifiedKey } from "../auth/Pgp.methods";
import { getSiteSettings } from "../settings/settings.methods";
import { orderCancelledNotification, orderCreatedNotification } from "./order.notifications";
import { orderNotification } from "./order.notifications";


@Resolver()
Expand Down Expand Up @@ -282,8 +282,7 @@ export class OrderResolver {
currency: walletCurrency,
});

// TODO remove after notification test
await orderCreatedNotification(createdOrUpdate);
await orderNotification(createdOrUpdate, "New order has been created");

return { data: createdOrUpdate, success: true };

Expand Down Expand Up @@ -322,8 +321,7 @@ export class OrderResolver {
reason,
});

// TODO delete after notification test
await orderCancelledNotification(currentOrder);
await orderNotification(currentOrder, "Order has been cancelled");

return { success: true, data: updateOrder };

Expand Down

0 comments on commit 0fb42c7

Please sign in to comment.