From 0c9abc14ec3233beb04162f20b7773a71cd772af Mon Sep 17 00:00:00 2001 From: Rahulcheryala Date: Wed, 3 Jun 2026 21:06:36 +0530 Subject: [PATCH] refactor: migrate services (notification, favorite) from apps/web to @packages/services --- .../services/favorite/favorite.service.ts | 62 --------- apps/web/core/services/favorite/index.ts | 7 - .../workspace-notification.service.ts | 124 ------------------ apps/web/core/store/favorite.store.ts | 20 +-- .../core/store/notifications/notification.ts | 18 +-- .../workspace-notifications.store.ts | 10 +- 6 files changed, 26 insertions(+), 215 deletions(-) delete mode 100644 apps/web/core/services/favorite/favorite.service.ts delete mode 100644 apps/web/core/services/favorite/index.ts delete mode 100644 apps/web/core/services/workspace-notification.service.ts diff --git a/apps/web/core/services/favorite/favorite.service.ts b/apps/web/core/services/favorite/favorite.service.ts deleted file mode 100644 index 6e8dd9f9a9..0000000000 --- a/apps/web/core/services/favorite/favorite.service.ts +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright (c) 2023-present Plane Software, Inc. and contributors - * SPDX-License-Identifier: AGPL-3.0-only - * See the LICENSE file for details. - */ - -import { API_BASE_URL } from "@plane/constants"; -import type { IFavorite } from "@plane/types"; -// helpers -// services -import { APIService } from "@/services/api.service"; -// types - -export class FavoriteService extends APIService { - constructor() { - super(API_BASE_URL); - } - - async addFavorite(workspaceSlug: string, data: Partial): Promise { - return this.post(`/api/workspaces/${workspaceSlug}/user-favorites/`, data) - .then((response) => response?.data) - .catch((error) => { - throw error?.response; - }); - } - - async updateFavorite(workspaceSlug: string, favoriteId: string, data: Partial): Promise { - return this.patch(`/api/workspaces/${workspaceSlug}/user-favorites/${favoriteId}/`, data) - .then((response) => response?.data) - .catch((error) => { - throw error?.response; - }); - } - - async deleteFavorite(workspaceSlug: string, favoriteId: string): Promise { - return this.delete(`/api/workspaces/${workspaceSlug}/user-favorites/${favoriteId}/`) - .then((response) => response?.data) - .catch((error) => { - throw error?.response; - }); - } - - async getFavorites(workspaceSlug: string): Promise { - return this.get(`/api/workspaces/${workspaceSlug}/user-favorites/`, { - params: { - all: true, - }, - }) - .then((response) => response?.data) - .catch((error) => { - throw error?.response?.data; - }); - } - - async getGroupedFavorites(workspaceSlug: string, favoriteId: string): Promise { - return this.get(`/api/workspaces/${workspaceSlug}/user-favorites/${favoriteId}/group/`) - .then((response) => response?.data) - .catch((error) => { - throw error?.response?.data; - }); - } -} diff --git a/apps/web/core/services/favorite/index.ts b/apps/web/core/services/favorite/index.ts deleted file mode 100644 index a789ea33c8..0000000000 --- a/apps/web/core/services/favorite/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Copyright (c) 2023-present Plane Software, Inc. and contributors - * SPDX-License-Identifier: AGPL-3.0-only - * See the LICENSE file for details. - */ - -export * from "./favorite.service"; diff --git a/apps/web/core/services/workspace-notification.service.ts b/apps/web/core/services/workspace-notification.service.ts deleted file mode 100644 index fc5c3f86ea..0000000000 --- a/apps/web/core/services/workspace-notification.service.ts +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Copyright (c) 2023-present Plane Software, Inc. and contributors - * SPDX-License-Identifier: AGPL-3.0-only - * See the LICENSE file for details. - */ - -/* eslint-disable no-useless-catch */ - -import { API_BASE_URL } from "@plane/constants"; -import type { - TNotificationPaginatedInfo, - TNotificationPaginatedInfoQueryParams, - TNotification, - TUnreadNotificationsCount, -} from "@plane/types"; -// helpers -// services -import { APIService } from "@/services/api.service"; - -export class WorkspaceNotificationService extends APIService { - constructor() { - super(API_BASE_URL); - } - - async fetchUnreadNotificationsCount(workspaceSlug: string): Promise { - try { - const { data } = await this.get(`/api/workspaces/${workspaceSlug}/users/notifications/unread/`); - return data || undefined; - } catch (error) { - throw error; - } - } - - async fetchNotifications( - workspaceSlug: string, - params: TNotificationPaginatedInfoQueryParams - ): Promise { - try { - const { data } = await this.get(`/api/workspaces/${workspaceSlug}/users/notifications`, { - params, - }); - return data || undefined; - } catch (error) { - throw error; - } - } - - async updateNotificationById( - workspaceSlug: string, - notificationId: string, - payload: Partial - ): Promise { - try { - const { data } = await this.patch( - `/api/workspaces/${workspaceSlug}/users/notifications/${notificationId}/`, - payload - ); - return data || undefined; - } catch (error) { - throw error; - } - } - - async markNotificationAsRead(workspaceSlug: string, notificationId: string): Promise { - try { - const { data } = await this.post(`/api/workspaces/${workspaceSlug}/users/notifications/${notificationId}/read/`); - return data || undefined; - } catch (error) { - throw error; - } - } - - async markNotificationAsUnread(workspaceSlug: string, notificationId: string): Promise { - try { - const { data } = await this.delete( - `/api/workspaces/${workspaceSlug}/users/notifications/${notificationId}/read/` - ); - return data || undefined; - } catch (error) { - throw error; - } - } - - async markNotificationAsArchived(workspaceSlug: string, notificationId: string): Promise { - try { - const { data } = await this.post( - `/api/workspaces/${workspaceSlug}/users/notifications/${notificationId}/archive/` - ); - return data || undefined; - } catch (error) { - throw error; - } - } - - async markNotificationAsUnArchived( - workspaceSlug: string, - notificationId: string - ): Promise { - try { - const { data } = await this.delete( - `/api/workspaces/${workspaceSlug}/users/notifications/${notificationId}/archive/` - ); - return data || undefined; - } catch (error) { - throw error; - } - } - - async markAllNotificationsAsRead( - workspaceSlug: string, - payload: TNotificationPaginatedInfoQueryParams - ): Promise { - try { - const { data } = await this.post(`/api/workspaces/${workspaceSlug}/users/notifications/mark-all-read/`, payload); - return data || undefined; - } catch (error) { - throw error; - } - } -} - -const workspaceNotificationService = new WorkspaceNotificationService(); - -export default workspaceNotificationService; diff --git a/apps/web/core/store/favorite.store.ts b/apps/web/core/store/favorite.store.ts index acf87ab022..5935a8483b 100644 --- a/apps/web/core/store/favorite.store.ts +++ b/apps/web/core/store/favorite.store.ts @@ -8,7 +8,7 @@ import { orderBy, uniqBy, set } from "lodash-es"; import { action, observable, makeObservable, runInAction, computed } from "mobx"; import { v4 as uuidv4 } from "uuid"; import type { IFavorite } from "@plane/types"; -import { FavoriteService } from "@/services/favorite"; +import { UserFavoriteService } from "@plane/services"; import type { CoreRootStore } from "./root.store"; export interface IFavoriteStore { @@ -81,7 +81,7 @@ export class FavoriteStore implements IFavoriteStore { removeFavoriteEntityFromStore: action, removeFromFavoriteFolder: action, }); - this.favoriteService = new FavoriteService(); + this.favoriteService = new UserFavoriteService(); this.rootStore = _rootStore; this.viewStore = _rootStore.projectView; this.projectStore = _rootStore.projectRoot.project; @@ -145,7 +145,7 @@ export class FavoriteStore implements IFavoriteStore { } this.favoriteIds = [id, ...this.favoriteIds]; }); - const response = await this.favoriteService.addFavorite(workspaceSlug, data); + const response = await this.favoriteService.add(workspaceSlug, data); // overwrite the temp id runInAction(() => { @@ -184,7 +184,7 @@ export class FavoriteStore implements IFavoriteStore { runInAction(() => { set(this.favoriteMap, [favoriteId], { ...this.favoriteMap[favoriteId], ...data }); }); - const response = await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, data); + const response = await this.favoriteService.update(workspaceSlug, favoriteId, data); return response; } catch (error) { @@ -205,7 +205,7 @@ export class FavoriteStore implements IFavoriteStore { */ moveFavoriteToFolder = async (workspaceSlug: string, favoriteId: string, data: Partial) => { try { - await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, data); + await this.favoriteService.update(workspaceSlug, favoriteId, data); runInAction(() => { // add parent of the favorite set(this.favoriteMap, [favoriteId, "parent"], data.parent); @@ -242,7 +242,7 @@ export class FavoriteStore implements IFavoriteStore { } } - await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { sequence: resultSequence }); + await this.favoriteService.update(workspaceSlug, favoriteId, { sequence: resultSequence }); runInAction(() => { set(this.favoriteMap, [favoriteId, "sequence"], resultSequence); @@ -255,7 +255,7 @@ export class FavoriteStore implements IFavoriteStore { removeFromFavoriteFolder = async (workspaceSlug: string, favoriteId: string) => { try { - await this.favoriteService.updateFavorite(workspaceSlug, favoriteId, { parent: null }); + await this.favoriteService.update(workspaceSlug, favoriteId, { parent: null }); runInAction(() => { //remove parent set(this.favoriteMap, [favoriteId, "parent"], null); @@ -308,7 +308,7 @@ export class FavoriteStore implements IFavoriteStore { const initialState = this.favoriteMap[favoriteId]; try { - await this.favoriteService.deleteFavorite(workspaceSlug, favoriteId); + await this.favoriteService.remove(workspaceSlug, favoriteId); runInAction(() => { delete this.favoriteMap[favoriteId]; if (entity_identifier) { @@ -400,7 +400,7 @@ export class FavoriteStore implements IFavoriteStore { fetchGroupedFavorites = async (workspaceSlug: string, favoriteId: string) => { if (!favoriteId) return []; try { - const response = await this.favoriteService.getGroupedFavorites(workspaceSlug, favoriteId); + const response = await this.favoriteService.groupedList(workspaceSlug, favoriteId); runInAction(() => { // add the favorites to the map response.forEach((favorite) => { @@ -426,7 +426,7 @@ export class FavoriteStore implements IFavoriteStore { */ fetchFavorite = async (workspaceSlug: string) => { try { - const favorites = await this.favoriteService.getFavorites(workspaceSlug); + const favorites = await this.favoriteService.list(workspaceSlug); runInAction(() => { favorites.forEach((favorite) => { set(this.favoriteMap, [favorite.id], favorite); diff --git a/apps/web/core/store/notifications/notification.ts b/apps/web/core/store/notifications/notification.ts index 16681086ae..de42557d71 100644 --- a/apps/web/core/store/notifications/notification.ts +++ b/apps/web/core/store/notifications/notification.ts @@ -9,7 +9,7 @@ import { set } from "lodash-es"; import { action, computed, makeObservable, observable, runInAction } from "mobx"; import type { IUserLite, TNotification, TNotificationData } from "@plane/types"; // services -import workspaceNotificationService from "@/services/workspace-notification.service"; +import { WorkspaceNotificationService } from "@plane/services"; // store import type { CoreRootStore } from "../root.store"; @@ -30,6 +30,8 @@ export interface INotification extends TNotification { unSnoozeNotification: (workspaceSlug: string) => Promise; } +const workspaceNotificationService = new WorkspaceNotificationService(); + export class Notification implements INotification { // observables id: string; @@ -176,7 +178,7 @@ export class Notification implements INotification { payload: Partial ): Promise => { try { - const notification = await workspaceNotificationService.updateNotificationById(workspaceSlug, this.id, payload); + const notification = await workspaceNotificationService.update(workspaceSlug, this.id, payload); if (notification) { runInAction(() => this.mutateNotification(notification)); } @@ -199,7 +201,7 @@ export class Notification implements INotification { }; this.store.workspaceNotification.setUnreadNotificationsCount("decrement"); runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.markNotificationAsRead(workspaceSlug, this.id); + const notification = await workspaceNotificationService.markAsRead(workspaceSlug, this.id); if (notification) { runInAction(() => this.mutateNotification(notification)); } @@ -224,7 +226,7 @@ export class Notification implements INotification { }; this.store.workspaceNotification.setUnreadNotificationsCount("increment"); runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.markNotificationAsUnread(workspaceSlug, this.id); + const notification = await workspaceNotificationService.markAsUnread(workspaceSlug, this.id); if (notification) { runInAction(() => this.mutateNotification(notification)); } @@ -248,7 +250,7 @@ export class Notification implements INotification { archived_at: new Date().toISOString(), }; runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.markNotificationAsArchived(workspaceSlug, this.id); + const notification = await workspaceNotificationService.archive(workspaceSlug, this.id); if (notification) { runInAction(() => this.mutateNotification(notification)); } @@ -271,7 +273,7 @@ export class Notification implements INotification { archived_at: undefined, }; runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.markNotificationAsUnArchived(workspaceSlug, this.id); + const notification = await workspaceNotificationService.unarchive(workspaceSlug, this.id); if (notification) { runInAction(() => this.mutateNotification(notification)); } @@ -295,7 +297,7 @@ export class Notification implements INotification { snoozed_till: snoozeTill.toISOString(), }; runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.updateNotificationById(workspaceSlug, this.id, payload); + const notification = await workspaceNotificationService.update(workspaceSlug, this.id, payload); return notification; } catch (error) { runInAction(() => this.mutateNotification({ snoozed_till: currentNotificationSnoozeTill })); @@ -315,7 +317,7 @@ export class Notification implements INotification { snoozed_till: undefined, }; runInAction(() => this.mutateNotification(payload)); - const notification = await workspaceNotificationService.updateNotificationById(workspaceSlug, this.id, payload); + const notification = await workspaceNotificationService.update(workspaceSlug, this.id, payload); return notification; } catch (error) { runInAction(() => this.mutateNotification({ snoozed_till: currentNotificationSnoozeTill })); diff --git a/apps/web/core/store/notifications/workspace-notifications.store.ts b/apps/web/core/store/notifications/workspace-notifications.store.ts index b11f563b29..5cfa3bfc2c 100644 --- a/apps/web/core/store/notifications/workspace-notifications.store.ts +++ b/apps/web/core/store/notifications/workspace-notifications.store.ts @@ -21,7 +21,7 @@ import type { // helpers import { convertToEpoch } from "@plane/utils"; // services -import workspaceNotificationService from "@/services/workspace-notification.service"; +import { WorkspaceNotificationService } from "@plane/services"; // store import type { INotification } from "@/store/notifications/notification"; import { Notification } from "@/store/notifications/notification"; @@ -60,6 +60,8 @@ export interface IWorkspaceNotificationStore { markAllNotificationsAsRead: (workspaceId: string) => Promise; } +const workspaceNotificationService = new WorkspaceNotificationService(); + export class WorkspaceNotificationStore implements IWorkspaceNotificationStore { // constants paginatedCount = 300; @@ -316,7 +318,7 @@ export class WorkspaceNotificationStore implements IWorkspaceNotificationStore { */ getUnreadNotificationsCount = async (workspaceSlug: string): Promise => { try { - const unreadNotificationCount = await workspaceNotificationService.fetchUnreadNotificationsCount(workspaceSlug); + const unreadNotificationCount = await workspaceNotificationService.getUnreadCount(workspaceSlug); if (unreadNotificationCount) runInAction(() => { set(this, "unreadNotificationsCount", unreadNotificationCount); @@ -343,7 +345,7 @@ export class WorkspaceNotificationStore implements IWorkspaceNotificationStore { try { const queryParams = this.generateNotificationQueryParams(queryParamType); await this.getUnreadNotificationsCount(workspaceSlug); - const notificationResponse = await workspaceNotificationService.fetchNotifications(workspaceSlug, queryParams); + const notificationResponse = await workspaceNotificationService.list(workspaceSlug, queryParams); if (notificationResponse) { const { results, ...paginationInfo } = notificationResponse; runInAction(() => { @@ -377,7 +379,7 @@ export class WorkspaceNotificationStore implements IWorkspaceNotificationStore { archived: queryParams.archived, read: queryParams.read, }; - await workspaceNotificationService.markAllNotificationsAsRead(workspaceSlug, params); + await workspaceNotificationService.markAllAsRead(workspaceSlug, params); runInAction(() => { update( this.unreadNotificationsCount,