diff --git a/CommonUI/src/Utils/ModelAPI/ModelAPI.ts b/CommonUI/src/Utils/ModelAPI/ModelAPI.ts index 78b355a414..e5a892180d 100644 --- a/CommonUI/src/Utils/ModelAPI/ModelAPI.ts +++ b/CommonUI/src/Utils/ModelAPI/ModelAPI.ts @@ -39,42 +39,43 @@ export interface RequestOptions { } export default class ModelAPI { - public static async create( + public static async create(data: { model: TBaseModel, - modelType: { new (): TBaseModel }, + modelType: { new(): TBaseModel }, requestOptions?: RequestOptions | undefined - ): Promise< + }): Promise< HTTPResponse> > { - return await ModelAPI.createOrUpdate( - model, - modelType, - FormType.Create, - {}, - requestOptions + return await ModelAPI.createOrUpdate({ + model: data.model, + modelType: data.modelType, + formType: FormType.Create, + miscDataProps: {}, + requestOptions: data.requestOptions + } ); } - public static async update( + public static async update(data: { model: TBaseModel, - modelType: { new (): TBaseModel } - ): Promise< + modelType: { new(): TBaseModel } + }): Promise< HTTPResponse> > { - return await ModelAPI.createOrUpdate(model, modelType, FormType.Update); + return await ModelAPI.createOrUpdate({ model: data.model, modelType: data.modelType, formType: FormType.Update }); } - public static async updateById( - modelType: { new (): TBaseModel }, + public static async updateById(data: { + modelType: { new(): TBaseModel }, id: ObjectID, data: JSONObject, apiUrlOverride?: URL, requestOptions?: RequestOptions - ): Promise< + }): Promise< HTTPResponse> > { - const model: BaseModel = new modelType(); - let apiUrl: URL | null = apiUrlOverride || null; + const model: BaseModel = new data.modelType(); + let apiUrl: URL | null = data.apiUrlOverride || null; if (!apiUrl) { const apiPath: Route | null = model.getCrudApiPath(); @@ -87,7 +88,7 @@ export default class ModelAPI { apiUrl = URL.fromURL(DASHBOARD_API_URL).addRoute(apiPath); } - apiUrl = apiUrl.addRoute(`/${id.toString()}`); + apiUrl = apiUrl.addRoute(`/${data.id.toString()}`); const result: HTTPResponse< JSONObject | JSONArray | TBaseModel | Array @@ -97,9 +98,9 @@ export default class ModelAPI { HTTPMethod.PUT, apiUrl, { - data: data, + data: data.data, }, - this.getCommonHeaders(requestOptions) + this.getCommonHeaders(data.requestOptions) ); if (result.isSuccess()) { @@ -111,17 +112,17 @@ export default class ModelAPI { throw result; } - public static async createOrUpdate( + public static async createOrUpdate(data: { model: TBaseModel, - modelType: { new (): TBaseModel }, + modelType: { new(): TBaseModel }, formType: FormType, miscDataProps?: JSONObject, requestOptions?: RequestOptions | undefined - ): Promise> { - let apiUrl: URL | null = requestOptions?.overrideRequestUrl || null; + }): Promise> { + let apiUrl: URL | null = data.requestOptions?.overrideRequestUrl || null; if (!apiUrl) { - const apiPath: Route | null = model.getCrudApiPath(); + const apiPath: Route | null = data.model.getCrudApiPath(); if (!apiPath) { throw new BadDataException( 'This model does not support create or update operations.' @@ -132,10 +133,10 @@ export default class ModelAPI { } const httpMethod: HTTPMethod = - formType === FormType.Create ? HTTPMethod.POST : HTTPMethod.PUT; + data.formType === FormType.Create ? HTTPMethod.POST : HTTPMethod.PUT; if (httpMethod === HTTPMethod.PUT) { - apiUrl = apiUrl.addRoute(`/${model._id}`); + apiUrl = apiUrl.addRoute(`/${data.model._id}`); } const apiResult: HTTPErrorResponse | HTTPResponse = @@ -144,13 +145,13 @@ export default class ModelAPI { apiUrl, { data: JSONFunctions.serialize( - BaseModel.toJSON(model, modelType) + BaseModel.toJSON(data.model, data.modelType) ), - miscDataProps: miscDataProps || {}, + miscDataProps: data.miscDataProps || {}, }, { - ...this.getCommonHeaders(requestOptions), - ...(requestOptions?.requestHeaders || {}), + ...this.getCommonHeaders(data.requestOptions), + ...(data.requestOptions?.requestHeaders || {}), } ); @@ -165,7 +166,7 @@ export default class ModelAPI { delete (result.data as any)['_miscData']; } - result.data = BaseModel.fromJSONObject(result.data, modelType); + result.data = BaseModel.fromJSONObject(result.data, data.modelType); return result; } @@ -175,16 +176,16 @@ export default class ModelAPI { throw apiResult; } - public static async getList( - modelType: { new (): TBaseModel }, + public static async getList(data: { + modelType: { new(): TBaseModel }, query: Query, limit: number, skip: number, select: Select, sort: Sort, requestOptions?: RequestOptions - ): Promise> { - const model: TBaseModel = new modelType(); + }): Promise> { + const model: TBaseModel = new data.modelType(); const apiPath: Route | null = model.getCrudApiPath(); if (!apiPath) { throw new BadDataException( @@ -196,8 +197,8 @@ export default class ModelAPI { .addRoute(apiPath) .addRoute('/get-list'); - if (requestOptions?.overrideRequestUrl) { - apiUrl = requestOptions.overrideRequestUrl; + if (data.requestOptions?.overrideRequestUrl) { + apiUrl = data.requestOptions.overrideRequestUrl; } if (!apiUrl) { @@ -207,8 +208,8 @@ export default class ModelAPI { } const headers: Dictionary = - this.getCommonHeaders(requestOptions); - if (requestOptions && requestOptions.isMultiTenantRequest) { + this.getCommonHeaders(data.requestOptions); + if (data.requestOptions && data.requestOptions.isMultiTenantRequest) { headers['isMultiTenantRequest'] = 'true'; } @@ -217,21 +218,21 @@ export default class ModelAPI { HTTPMethod.POST, apiUrl, { - query: JSONFunctions.serialize(query as JSONObject), - select: JSONFunctions.serialize(select as JSONObject), - sort: JSONFunctions.serialize(sort as JSONObject), + query: JSONFunctions.serialize(data.query as JSONObject), + select: JSONFunctions.serialize(data.select as JSONObject), + sort: JSONFunctions.serialize(data.sort as JSONObject), }, headers, { - limit: limit.toString(), - skip: skip.toString(), + limit: data.limit.toString(), + skip: data.skip.toString(), } ); if (result.isSuccess()) { const list: Array = BaseModel.fromJSONArray( result.data as JSONArray, - modelType + data.modelType ); return { @@ -247,12 +248,12 @@ export default class ModelAPI { throw result; } - public static async count( - modelType: { new (): TBaseModel }, + public static async count(data: { + modelType: { new(): TBaseModel }, query: Query, requestOptions?: RequestOptions | undefined - ): Promise { - const model: TBaseModel = new modelType(); + }): Promise { + const model: TBaseModel = new data.modelType(); const apiPath: Route | null = model.getCrudApiPath(); if (!apiPath) { throw new BadDataException( @@ -264,8 +265,8 @@ export default class ModelAPI { .addRoute(apiPath) .addRoute('/count'); - if (requestOptions?.overrideRequestUrl) { - apiUrl = requestOptions.overrideRequestUrl; + if (data.requestOptions?.overrideRequestUrl) { + apiUrl = data.requestOptions.overrideRequestUrl; } if (!apiUrl) { @@ -275,8 +276,8 @@ export default class ModelAPI { } const headers: Dictionary = - this.getCommonHeaders(requestOptions); - if (requestOptions && requestOptions.isMultiTenantRequest) { + this.getCommonHeaders(data.requestOptions); + if (data.requestOptions && data.requestOptions.isMultiTenantRequest) { headers['is-multi-tenant-query'] = 'true'; } @@ -285,7 +286,7 @@ export default class ModelAPI { HTTPMethod.POST, apiUrl, { - query: JSONFunctions.serialize(query as JSONObject), + query: JSONFunctions.serialize(data.query as JSONObject), }, headers ); @@ -331,13 +332,13 @@ export default class ModelAPI { return headers; } - public static async getItem( - modelType: { new (): TBaseModel }, + public static async getItem(data: { + modelType: { new(): TBaseModel }, id: ObjectID, select: Select, requestOptions?: RequestOptions | undefined - ): Promise { - const apiPath: Route | null = new modelType().getCrudApiPath(); + }): Promise { + const apiPath: Route | null = new data.modelType().getCrudApiPath(); if (!apiPath) { throw new BadDataException( 'This model does not support get operations.' @@ -346,11 +347,11 @@ export default class ModelAPI { let apiUrl: URL = URL.fromURL(DASHBOARD_API_URL) .addRoute(apiPath) - .addRoute('/' + id.toString()) + .addRoute('/' + data.id.toString()) .addRoute('/get-item'); - if (requestOptions?.overrideRequestUrl) { - apiUrl = requestOptions.overrideRequestUrl; + if (data.requestOptions?.overrideRequestUrl) { + apiUrl = data.requestOptions.overrideRequestUrl; } if (!apiUrl) { @@ -359,29 +360,31 @@ export default class ModelAPI { ); } - return this.post(modelType, apiUrl, select, requestOptions); + return this.post({ + modelType: data.modelType, apiUrl: apiUrl, select: data.select, requestOptions: data.requestOptions + }); } - public static async post( - modelType: { new (): TBaseModel }, + public static async post(data: { + modelType: { new(): TBaseModel }, apiUrl: URL, select?: Select | undefined, requestOptions?: RequestOptions | undefined - ): Promise { + }): Promise { const result: HTTPResponse | HTTPErrorResponse = await API.fetch( HTTPMethod.POST, - apiUrl, + data.apiUrl, { - select: JSONFunctions.serialize(select as JSONObject) || {}, + select: JSONFunctions.serialize(data.select as JSONObject) || {}, }, - this.getCommonHeaders(requestOptions) + this.getCommonHeaders(data.requestOptions) ); if (result.isSuccess()) { return BaseModel.fromJSONObject( result.data as JSONObject, - modelType + data.modelType ); } @@ -390,12 +393,12 @@ export default class ModelAPI { throw result; } - public static async deleteItem( - modelType: { new (): TBaseModel }, + public static async deleteItem(data: { + modelType: { new(): TBaseModel }, id: ObjectID, requestOptions?: RequestOptions | undefined - ): Promise { - const apiPath: Route | null = new modelType().getCrudApiPath(); + }): Promise { + const apiPath: Route | null = new data.modelType().getCrudApiPath(); if (!apiPath) { throw new BadDataException( 'This model does not support delete operations.' @@ -404,7 +407,7 @@ export default class ModelAPI { const apiUrl: URL = URL.fromURL(DASHBOARD_API_URL) .addRoute(apiPath) - .addRoute('/' + id.toString()); + .addRoute('/' + data.id.toString()); if (!apiUrl) { throw new BadDataException( @@ -417,7 +420,7 @@ export default class ModelAPI { HTTPMethod.DELETE, apiUrl, undefined, - this.getCommonHeaders(requestOptions) + this.getCommonHeaders(data.requestOptions) ); if (result.isSuccess()) { @@ -432,8 +435,8 @@ export default class ModelAPI { private static checkStatusCode( result: | HTTPResponse< - TBaseModel | JSONObject | JSONArray | Array - > + TBaseModel | JSONObject | JSONArray | Array + > | HTTPErrorResponse ): void { if (result.statusCode === 406) {