[WEB-522] chore: Move the estimates store and service out of core (#4896)

* chore: moved the estimate store from core to ce and ee

* chore: moved the estimate service from core to ce and ee

* chore: updated constructors from private to public in estimate store

* chore: exported estimate service
This commit is contained in:
guru_sainath 2024-06-20 19:29:09 +05:30 committed by GitHub
parent 94e6fd4b29
commit c0ad5952df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 28 additions and 195 deletions

1
web/ce/services/index.ts Normal file
View file

@ -0,0 +1 @@
export * from "./project";

View file

@ -0,0 +1,106 @@
/* eslint-disable no-useless-catch */
// types
import { IEstimate, IEstimateFormData, IEstimatePoint } from "@plane/types";
// helpers
import { API_BASE_URL } from "@/helpers/common.helper";
// services
import { APIService } from "@/services/api.service";
export class EstimateService extends APIService {
constructor() {
super(API_BASE_URL);
}
async fetchWorkspaceEstimates(workspaceSlug: string): Promise<IEstimate[] | undefined> {
try {
const { data } = await this.get(`/api/workspaces/${workspaceSlug}/estimates/`);
return data || undefined;
} catch (error) {
throw error;
}
}
async fetchProjectEstimates(workspaceSlug: string, projectId: string): Promise<IEstimate[] | undefined> {
try {
const { data } = await this.get(`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/`);
return data || undefined;
} catch (error) {
throw error;
}
}
async fetchEstimateById(
workspaceSlug: string,
projectId: string,
estimateId: string
): Promise<IEstimate | undefined> {
try {
const { data } = await this.get(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/`
);
return data || undefined;
} catch (error) {
throw error;
}
}
async createEstimate(
workspaceSlug: string,
projectId: string,
payload: IEstimateFormData
): Promise<IEstimate | undefined> {
try {
const { data } = await this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/`, payload);
return data || undefined;
} catch (error) {
throw error;
}
}
async deleteEstimate(workspaceSlug: string, projectId: string, estimateId: string): Promise<void> {
try {
await this.delete(`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/`);
} catch (error) {
throw error;
}
}
async createEstimatePoint(
workspaceSlug: string,
projectId: string,
estimateId: string,
payload: Partial<IEstimatePoint>
): Promise<IEstimatePoint | undefined> {
try {
const { data } = await this.post(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/estimate-points/`,
payload
);
return data || undefined;
} catch (error) {
throw error;
}
}
async updateEstimatePoint(
workspaceSlug: string,
projectId: string,
estimateId: string,
estimatePointId: string,
payload: Partial<IEstimatePoint>
): Promise<IEstimatePoint | undefined> {
try {
const { data } = await this.patch(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/estimate-points/${estimatePointId}/`,
payload
);
return data || undefined;
} catch (error) {
throw error;
}
}
}
const estimateService = new EstimateService();
export default estimateService;

View file

@ -0,0 +1 @@
export * from "./estimate.service";

View file

@ -0,0 +1,158 @@
/* eslint-disable no-useless-catch */
import orderBy from "lodash/orderBy";
import set from "lodash/set";
// import unset from "lodash/unset";
import { action, computed, makeObservable, observable, runInAction } from "mobx";
import { computedFn } from "mobx-utils";
// types
import { IEstimate as IEstimateType, IEstimatePoint as IEstimatePointType, TEstimateSystemKeys } from "@plane/types";
// plane web services
import estimateService from "@/plane-web/services/project/estimate.service";
// store
import { IEstimatePoint, EstimatePoint } from "@/store/estimates/estimate-point";
import { CoreRootStore } from "@/store/root.store";
type TErrorCodes = {
status: string;
message?: string;
};
export interface IEstimate extends Omit<IEstimateType, "points"> {
// observables
error: TErrorCodes | undefined;
estimatePoints: Record<string, IEstimatePoint>;
// computed
asJson: Omit<IEstimateType, "points">;
estimatePointIds: string[] | undefined;
estimatePointById: (estimatePointId: string) => IEstimatePointType | undefined;
// actions
creteEstimatePoint: (
workspaceSlug: string,
projectId: string,
payload: Partial<IEstimatePointType>
) => Promise<IEstimatePointType | undefined>;
}
export class Estimate implements IEstimate {
// data model observables
id: string | undefined = undefined;
name: string | undefined = undefined;
description: string | undefined = undefined;
type: TEstimateSystemKeys | undefined = undefined;
workspace: string | undefined = undefined;
project: string | undefined = undefined;
last_used: boolean | undefined = undefined;
created_at: Date | undefined = undefined;
updated_at: Date | undefined = undefined;
created_by: string | undefined = undefined;
updated_by: string | undefined = undefined;
// observables
error: TErrorCodes | undefined = undefined;
estimatePoints: Record<string, IEstimatePoint> = {};
constructor(
public store: CoreRootStore,
public data: IEstimateType
) {
makeObservable(this, {
// data model observables
id: observable.ref,
name: observable.ref,
description: observable.ref,
type: observable.ref,
workspace: observable.ref,
project: observable.ref,
last_used: observable.ref,
created_at: observable.ref,
updated_at: observable.ref,
created_by: observable.ref,
updated_by: observable.ref,
// observables
error: observable.ref,
estimatePoints: observable,
// computed
asJson: computed,
estimatePointIds: computed,
// actions
creteEstimatePoint: action,
});
this.id = this.data.id;
this.name = this.data.name;
this.description = this.data.description;
this.type = this.data.type;
this.workspace = this.data.workspace;
this.project = this.data.project;
this.last_used = this.data.last_used;
this.created_at = this.data.created_at;
this.updated_at = this.data.updated_at;
this.created_by = this.data.created_by;
this.updated_by = this.data.updated_by;
this.data.points?.forEach((estimationPoint) => {
if (estimationPoint.id)
set(this.estimatePoints, [estimationPoint.id], new EstimatePoint(this.store, this.data, estimationPoint));
});
}
// computed
get asJson() {
return {
id: this.id,
name: this.name,
description: this.description,
type: this.type,
workspace: this.workspace,
project: this.project,
last_used: this.last_used,
created_at: this.created_at,
updated_at: this.updated_at,
created_by: this.created_by,
updated_by: this.updated_by,
};
}
get estimatePointIds() {
const { estimatePoints } = this;
if (!estimatePoints) return undefined;
let currentEstimatePoints = Object.values(estimatePoints).filter(
(estimatePoint) => estimatePoint?.estimate === this.id
);
currentEstimatePoints = orderBy(currentEstimatePoints, ["key"], "asc");
const estimatePointIds = currentEstimatePoints.map((estimatePoint) => estimatePoint.id) as string[];
return estimatePointIds ?? undefined;
}
estimatePointById = computedFn((estimatePointId: string) => {
if (!estimatePointId) return undefined;
return this.estimatePoints[estimatePointId] ?? undefined;
});
// actions
/**
* @description create an estimate point
* @param { string } workspaceSlug
* @param { string } projectId
* @param { Partial<IEstimatePointType> } payload
* @returns { IEstimatePointType | undefined }
*/
creteEstimatePoint = async (
workspaceSlug: string,
projectId: string,
payload: Partial<IEstimatePointType>
): Promise<IEstimatePointType | undefined> => {
try {
if (!this.id || !payload) return;
const estimatePoint = await estimateService.createEstimatePoint(workspaceSlug, projectId, this.id, payload);
if (estimatePoint) {
runInAction(() => {
if (estimatePoint.id) {
set(this.estimatePoints, [estimatePoint.id], new EstimatePoint(this.store, this.data, estimatePoint));
}
});
}
} catch (error) {
throw error;
}
};
}