From feea58313c5979d4eed89bdbbad717601fab5478 Mon Sep 17 00:00:00 2001 From: Ayobami Akingbade Date: Thu, 4 Jan 2024 16:49:27 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(validations):=20c?= =?UTF-8?q?entralize=20no=20allowed=20on=20demo=20validations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard-widgets.controller.ts | 83 ------------------- src/backend/entities/entities.controller.ts | 20 ----- .../validations/implementations/index.ts | 2 + .../implementations/not-allowed-on-demo.ts | 10 +++ src/backend/lib/request/validations/types.ts | 1 + src/backend/users/users.controller.ts | 36 +------- src/backend/users/users.service.ts | 12 +-- .../components/ListManager/index.tsx | 2 +- src/pages/api/account/[username]/index.ts | 10 +-- .../api/account/[username]/reset-password.ts | 7 +- src/pages/api/account/change-password.ts | 37 +++++---- src/pages/api/account/index.ts | 6 +- src/pages/api/account/mine.ts | 3 +- .../[dashboardId]/[widgetId]/index.ts | 9 +- .../api/dashboards/[dashboardId]/index.ts | 12 ++- src/pages/api/dashboards/script.ts | 10 ++- src/pages/api/entities/[entity]/relations.ts | 4 +- src/pages/api/entities/active.ts | 4 +- src/pages/api/entities/list.ts | 4 +- src/pages/api/upload/index.ts | 59 +++++++------ 20 files changed, 113 insertions(+), 218 deletions(-) delete mode 100644 src/backend/dashboard-widgets/dashboard-widgets.controller.ts create mode 100644 src/backend/lib/request/validations/implementations/not-allowed-on-demo.ts diff --git a/src/backend/dashboard-widgets/dashboard-widgets.controller.ts b/src/backend/dashboard-widgets/dashboard-widgets.controller.ts deleted file mode 100644 index 602e5aeeb..000000000 --- a/src/backend/dashboard-widgets/dashboard-widgets.controller.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { IWidgetConfig } from "shared/types/dashboard"; -import { IAccountProfile } from "shared/types/user"; -import { BadRequestError } from "backend/lib/errors"; -import { - DashboardWidgetsApiService, - dashboardWidgetsApiService, -} from "./dashboard-widgets.service"; - -export class DashboardWidgetsApiController { - constructor( - private _dashboardWidgetsApiService: DashboardWidgetsApiService - ) {} - - async listDashboardWidgets(dashboardId: string, userRole: string) { - return await this._dashboardWidgetsApiService.listDashboardWidgets( - dashboardId, - userRole - ); - } - - async runWidgetScript( - widgetId: string, - currentUser: IAccountProfile, - relativeDate: string - ) { - return await this._dashboardWidgetsApiService.runWidgetScript( - widgetId, - currentUser, - relativeDate - ); - } - - async runScript( - script: string, - currentUser: IAccountProfile, - relativeDate: string - ) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - throw new BadRequestError("Cannot run script in demo mode"); - } - - return await this._dashboardWidgetsApiService.runScript( - script, - currentUser, - relativeDate - ); - } - - async createWidget(widget: IWidgetConfig, dashboardId: string) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - throw new BadRequestError("Cannot create widget in demo mode"); - } - - await this._dashboardWidgetsApiService.createWidget(widget, dashboardId); - } - - async updateWidget(widgetId: string, widget: IWidgetConfig) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - throw new BadRequestError("Cannot update widget in demo mode"); - } - - await this._dashboardWidgetsApiService.updateWidget(widgetId, widget); - } - - async removeWidget(widgetId: string, dashboardId: string) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - throw new BadRequestError("Cannot remove widget in demo mode"); - } - - await this._dashboardWidgetsApiService.removeWidget(widgetId, dashboardId); - } - - async updateWidgetList(dashboardId: string, widgetList: string[]) { - await this._dashboardWidgetsApiService.updateWidgetList( - dashboardId, - widgetList - ); - } -} - -export const dashboardWidgetsApiController = new DashboardWidgetsApiController( - dashboardWidgetsApiService -); diff --git a/src/backend/entities/entities.controller.ts b/src/backend/entities/entities.controller.ts index 8ed665a5c..1ccb32789 100644 --- a/src/backend/entities/entities.controller.ts +++ b/src/backend/entities/entities.controller.ts @@ -1,5 +1,3 @@ -import { ILabelValue } from "shared/types/options"; -import { IEntityRelation } from "shared/types/db"; import { ConfigurationApiService, configurationApiService, @@ -12,14 +10,6 @@ export class EntitiesApiController { private _configurationApiService: ConfigurationApiService ) {} - async getActiveEntities(): Promise { - return await this._entitiesApiService.getActiveEntities(); - } - - async listAllEntities(): Promise { - return await this._entitiesApiService.getAllEntities(); - } - async listAllEntityRelations(entity: string): Promise { const [entityRelations, disabledEntities] = await Promise.all([ this._entitiesApiService.getEntityRelations(entity), @@ -32,16 +22,6 @@ export class EntitiesApiController { return allowedEntityRelation.map(({ table }) => table); } - - async getEntityRelations( - entity: string, - userRole: string - ): Promise { - return await this._entitiesApiService.getEntityRelationsForUserRole( - entity, - userRole - ); - } } export const entitiesApiController = new EntitiesApiController( diff --git a/src/backend/lib/request/validations/implementations/index.ts b/src/backend/lib/request/validations/implementations/index.ts index a1a14f32e..b1ffc5735 100644 --- a/src/backend/lib/request/validations/implementations/index.ts +++ b/src/backend/lib/request/validations/implementations/index.ts @@ -14,6 +14,7 @@ import { withPasswordValidationImpl as withPassword } from "./with-password"; import { authenticatedUserValidationImpl as authenticatedUser } from "./authenticated-user"; import { requestQueriesValidationImpl as requestQueries } from "./request-queries"; import { rawRequestValidationImpl as rawRequest } from "./raw-request"; +import { notAllowedOnDemoValidationImpl as notAllowedOnDemo } from "./not-allowed-on-demo"; import { ValidationImplType } from "./types"; import { PortalValidationImpl } from "./portal"; @@ -31,6 +32,7 @@ export const ValidationImpl: Record< requestQuery, canUser, rawRequest, + notAllowedOnDemo, requestQueries, authenticatedUser, entity, diff --git a/src/backend/lib/request/validations/implementations/not-allowed-on-demo.ts b/src/backend/lib/request/validations/implementations/not-allowed-on-demo.ts new file mode 100644 index 000000000..aa64f0736 --- /dev/null +++ b/src/backend/lib/request/validations/implementations/not-allowed-on-demo.ts @@ -0,0 +1,10 @@ +import { BadRequestError } from "backend/lib/errors"; +import { ValidationImplType } from "./types"; + +export const notAllowedOnDemoValidationImpl: ValidationImplType< + void +> = async () => { + if (process.env.NEXT_PUBLIC_IS_DEMO) { + throw new BadRequestError("This service is not available on the demo site"); + } +}; diff --git a/src/backend/lib/request/validations/types.ts b/src/backend/lib/request/validations/types.ts index e73b6d51d..36592ecb4 100644 --- a/src/backend/lib/request/validations/types.ts +++ b/src/backend/lib/request/validations/types.ts @@ -11,6 +11,7 @@ export type ValidationKeys = { | "rawRequest" | "paginationFilter" | "canUser" + | "notAllowedOnDemo" | "crudEnabled" | "requestBody" | "requestQuery" diff --git a/src/backend/users/users.controller.ts b/src/backend/users/users.controller.ts index fe758d4f0..b2c6f124d 100644 --- a/src/backend/users/users.controller.ts +++ b/src/backend/users/users.controller.ts @@ -2,14 +2,8 @@ import { UnauthorizedError } from "backend/lib/errors"; import { RolesApiService, rolesApiService } from "backend/roles/roles.service"; import { REQUEST_ERROR_CODES } from "shared/constants/auth"; import { ISignInForm } from "shared/form-schemas/auth/signin"; -import { IChangePasswordForm } from "shared/form-schemas/profile/password"; -import { IResetPasswordForm } from "shared/form-schemas/users/reset-password"; import { ISuccessfullAuthenticationResponse } from "shared/types/auth/portal"; -import { - IAccountProfile, - IAccountUser, - IAuthenticatedUserBag, -} from "shared/types/user"; +import { IAuthenticatedUserBag } from "shared/types/user"; import { UsersApiService, usersApiService } from "./users.service"; export class UsersApiController { @@ -24,22 +18,6 @@ export class UsersApiController { return await this._usersService.tryAuthenticate(authCredentials); } - async listUsers() { - return await this._usersService.listUsers(); - } - - async createUser(user: IAccountUser) { - await this._usersService.registerUser(user); - } - - async removeUser(username: string, myUsername: string) { - await this._usersService.removeUser(username, myUsername); - } - - async getUserProfile(username: string) { - return await this._usersService.getUser(username); - } - async getAuthenticatedUserBag( authenticatedUsername: string ): Promise { @@ -62,18 +40,6 @@ export class UsersApiController { ); } } - - async resetPassword(username: string, input: IResetPasswordForm) { - await this._usersService.resetPassword(username, input.password); - } - - async updatePassword(username: string, input: IChangePasswordForm) { - await this._usersService.changePassword(username, input); - } - - async updateProfile(username: string, userDetails: IAccountProfile) { - await this._usersService.updateUser(username, userDetails); - } } export const usersApiController = new UsersApiController( diff --git a/src/backend/users/users.service.ts b/src/backend/users/users.service.ts index b1ae5b12c..252a33cd1 100644 --- a/src/backend/users/users.service.ts +++ b/src/backend/users/users.service.ts @@ -5,6 +5,7 @@ import { IApplicationService } from "backend/types"; import { IAccountUser, IAccountProfile } from "shared/types/user"; import { ISuccessfullAuthenticationResponse } from "shared/types/auth/portal"; import { noop } from "shared/lib/noop"; +import { IResetPasswordForm } from "shared/form-schemas/users/reset-password"; import { getPortalAuthenticationResponse } from "./portal"; import { generateAuthTokenForUsername } from "./utils"; import { usersPersistenceService } from "./shared"; @@ -101,10 +102,6 @@ export class UsersApiService implements IApplicationService { newPassword: string; } ) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - return; - } - try { await this.checkUserPassword({ username, @@ -119,12 +116,9 @@ export class UsersApiService implements IApplicationService { }); } - async resetPassword(username: string, newPassword: string) { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - return; - } + async resetPassword(username: string, newPassword: IResetPasswordForm) { await this.updateUser(username, { - password: await HashService.make(newPassword), + password: await HashService.make(newPassword.password), }); } diff --git a/src/frontend/design-system/components/ListManager/index.tsx b/src/frontend/design-system/components/ListManager/index.tsx index 63d613fa8..90171a3fd 100644 --- a/src/frontend/design-system/components/ListManager/index.tsx +++ b/src/frontend/design-system/components/ListManager/index.tsx @@ -64,7 +64,7 @@ export function ListManager>({ const onSortEnd = (oldOrder: number, newOrder: number) => { const newOrderItems = arrayMoveImmutable(itemsData, oldOrder, newOrder); setItemsData(newOrderItems); - sort?.on(newOrderItems.map((item) => item[sort.key] as string)); + sort?.on(newOrderItems.map((item) => item[sort.key] as unknown as string)); }; useEffect(() => { diff --git a/src/pages/api/account/[username]/index.ts b/src/pages/api/account/[username]/index.ts index e35a42145..18182ae15 100644 --- a/src/pages/api/account/[username]/index.ts +++ b/src/pages/api/account/[username]/index.ts @@ -1,8 +1,8 @@ -import { usersApiController } from "backend/users/users.controller"; import { USER_PERMISSIONS } from "shared/constants/user"; import { UPDATE_USER_FORM_SCHEMA } from "shared/form-schemas/users/update"; import { IAccountProfile } from "shared/types/user"; import { requestHandler } from "backend/lib/request"; +import { usersApiService } from "backend/users/users.service"; const REQUEST_QUERY_FIELD = "username"; @@ -15,9 +15,7 @@ export default requestHandler( options: REQUEST_QUERY_FIELD, }, ]); - return await usersApiController.getUserProfile( - validatedRequest.requestQuery - ); + return await usersApiService.getUser(validatedRequest.requestQuery); }, DELETE: async (getValidatedRequest) => { @@ -28,7 +26,7 @@ export default requestHandler( options: REQUEST_QUERY_FIELD, }, ]); - return await usersApiController.removeUser( + return await usersApiService.removeUser( validatedRequest.requestQuery, (validatedRequest.authenticatedUser as IAccountProfile).username ); @@ -44,7 +42,7 @@ export default requestHandler( options: REQUEST_QUERY_FIELD, }, ]); - return await usersApiController.updateProfile( + return await usersApiService.updateUser( validatedRequest.requestQuery, validatedRequest.requestBody ); diff --git a/src/pages/api/account/[username]/reset-password.ts b/src/pages/api/account/[username]/reset-password.ts index 8aed5e224..6beac0e48 100644 --- a/src/pages/api/account/[username]/reset-password.ts +++ b/src/pages/api/account/[username]/reset-password.ts @@ -1,7 +1,7 @@ -import { usersApiController } from "backend/users/users.controller"; import { RESET_PASSWORD_FORM_SCHEMA } from "shared/form-schemas/users/reset-password"; import { USER_PERMISSIONS } from "shared/constants/user"; import { requestHandler } from "backend/lib/request"; +import { usersApiService } from "backend/users/users.service"; export default requestHandler( { @@ -16,13 +16,16 @@ export default requestHandler( options: "username", }, ]); - return await usersApiController.resetPassword( + return await usersApiService.resetPassword( validatedRequest.requestQuery, validatedRequest.requestBody ); }, }, [ + { + _type: "notAllowedOnDemo", + }, { _type: "canUser", body: USER_PERMISSIONS.CAN_RESET_PASSWORD, diff --git a/src/pages/api/account/change-password.ts b/src/pages/api/account/change-password.ts index 8d2458a82..6a548de57 100644 --- a/src/pages/api/account/change-password.ts +++ b/src/pages/api/account/change-password.ts @@ -1,20 +1,27 @@ -import { usersApiController } from "backend/users/users.controller"; import { CHANGE_PASSWORD_FORM_SCHEMA } from "shared/form-schemas/profile/password"; import { IAccountProfile } from "shared/types/user"; import { requestHandler } from "backend/lib/request"; +import { usersApiService } from "backend/users/users.service"; -export default requestHandler({ - PATCH: async (getValidatedRequest) => { - const validatedRequest = await getValidatedRequest([ - "authenticatedUser", - { - _type: "requestBody", - options: CHANGE_PASSWORD_FORM_SCHEMA, - }, - ]); - return await usersApiController.updatePassword( - (validatedRequest.authenticatedUser as IAccountProfile).username, - validatedRequest.requestBody - ); +export default requestHandler( + { + PATCH: async (getValidatedRequest) => { + const validatedRequest = await getValidatedRequest([ + "authenticatedUser", + { + _type: "requestBody", + options: CHANGE_PASSWORD_FORM_SCHEMA, + }, + ]); + return await usersApiService.changePassword( + (validatedRequest.authenticatedUser as IAccountProfile).username, + validatedRequest.requestBody + ); + }, }, -}); + [ + { + _type: "notAllowedOnDemo", + }, + ] +); diff --git a/src/pages/api/account/index.ts b/src/pages/api/account/index.ts index a64cf43d3..ba0b0cef9 100644 --- a/src/pages/api/account/index.ts +++ b/src/pages/api/account/index.ts @@ -1,11 +1,11 @@ -import { usersApiController } from "backend/users/users.controller"; import { CREATE_USER_FORM_SCHEMA } from "shared/form-schemas/users/create"; import { USER_PERMISSIONS } from "shared/constants/user"; import { requestHandler } from "backend/lib/request"; +import { usersApiService } from "backend/users/users.service"; export default requestHandler({ GET: async () => { - return await usersApiController.listUsers(); + return await usersApiService.listUsers(); }, POST: async (getValidatedRequest) => { @@ -19,6 +19,6 @@ export default requestHandler({ options: CREATE_USER_FORM_SCHEMA, }, ]); - return await usersApiController.createUser(validatedRequest.requestBody); + return await usersApiService.registerUser(validatedRequest.requestBody); }, }); diff --git a/src/pages/api/account/mine.ts b/src/pages/api/account/mine.ts index a71f57449..364ff06c0 100644 --- a/src/pages/api/account/mine.ts +++ b/src/pages/api/account/mine.ts @@ -2,6 +2,7 @@ import { usersApiController } from "backend/users/users.controller"; import { UPDATE_PROFILE_FORM_SCHEMA } from "shared/form-schemas/profile/update"; import { IAccountProfile } from "shared/types/user"; import { requestHandler } from "backend/lib/request"; +import { usersApiService } from "backend/users/users.service"; export default requestHandler({ PATCH: async (getValidatedRequest) => { @@ -12,7 +13,7 @@ export default requestHandler({ options: UPDATE_PROFILE_FORM_SCHEMA, }, ]); - return await usersApiController.updateProfile( + return await usersApiService.updateUser( (validatedRequest.authenticatedUser as IAccountProfile).username, validatedRequest.requestBody ); diff --git a/src/pages/api/dashboards/[dashboardId]/[widgetId]/index.ts b/src/pages/api/dashboards/[dashboardId]/[widgetId]/index.ts index 36816d900..a2d8a944c 100644 --- a/src/pages/api/dashboards/[dashboardId]/[widgetId]/index.ts +++ b/src/pages/api/dashboards/[dashboardId]/[widgetId]/index.ts @@ -1,6 +1,6 @@ -import { dashboardWidgetsApiController } from "backend/dashboard-widgets/dashboard-widgets.controller"; import { USER_PERMISSIONS } from "shared/constants/user"; import { requestHandler } from "backend/lib/request"; +import { dashboardWidgetsApiService } from "backend/dashboard-widgets/dashboard-widgets.service"; export default requestHandler( { @@ -9,7 +9,7 @@ export default requestHandler( { _type: "requestQuery", options: "widgetId" }, { _type: "requestBody", options: {} }, ]); - return await dashboardWidgetsApiController.updateWidget( + return await dashboardWidgetsApiService.updateWidget( validatedRequest.requestQuery, validatedRequest.requestBody ); @@ -19,13 +19,16 @@ export default requestHandler( { _type: "requestQuery", options: "dashboardId" }, { _type: "requestBody", options: {} }, ]); - return await dashboardWidgetsApiController.removeWidget( + return await dashboardWidgetsApiService.removeWidget( validatedRequest.requestBody.widgetId, validatedRequest.requestQuery ); }, }, [ + { + _type: "notAllowedOnDemo", + }, { _type: "canUser", body: USER_PERMISSIONS.CAN_MANAGE_DASHBOARD, diff --git a/src/pages/api/dashboards/[dashboardId]/index.ts b/src/pages/api/dashboards/[dashboardId]/index.ts index d86da7e70..64f9cc89e 100644 --- a/src/pages/api/dashboards/[dashboardId]/index.ts +++ b/src/pages/api/dashboards/[dashboardId]/index.ts @@ -1,7 +1,7 @@ -import { dashboardWidgetsApiController } from "backend/dashboard-widgets/dashboard-widgets.controller"; import { USER_PERMISSIONS } from "shared/constants/user"; import { IAccountProfile } from "shared/types/user"; import { requestHandler } from "backend/lib/request"; +import { dashboardWidgetsApiService } from "backend/dashboard-widgets/dashboard-widgets.service"; const REQUEST_QUERY_FIELD = "dashboardId"; @@ -13,7 +13,7 @@ export default requestHandler( { _type: "requestQuery", options: REQUEST_QUERY_FIELD }, ]); - return await dashboardWidgetsApiController.listDashboardWidgets( + return await dashboardWidgetsApiService.listDashboardWidgets( validatedRequest.requestQuery, (validatedRequest.authenticatedUser as IAccountProfile).role ); @@ -23,7 +23,7 @@ export default requestHandler( { _type: "requestQuery", options: REQUEST_QUERY_FIELD }, { _type: "requestBody", options: {} }, ]); - return await dashboardWidgetsApiController.createWidget( + return await dashboardWidgetsApiService.createWidget( validatedRequest.requestBody, validatedRequest.requestQuery ); @@ -33,13 +33,17 @@ export default requestHandler( { _type: "requestQuery", options: REQUEST_QUERY_FIELD }, { _type: "requestBody", options: {} }, ]); - return await dashboardWidgetsApiController.updateWidgetList( + return await dashboardWidgetsApiService.updateWidgetList( validatedRequest.requestQuery, validatedRequest.requestBody ); }, }, [ + { + _type: "notAllowedOnDemo", + method: ["POST"], + }, { method: ["PATCH", "POST"], _type: "canUser", diff --git a/src/pages/api/dashboards/script.ts b/src/pages/api/dashboards/script.ts index 943d3709b..3afe0dc6f 100644 --- a/src/pages/api/dashboards/script.ts +++ b/src/pages/api/dashboards/script.ts @@ -1,7 +1,7 @@ -import { dashboardWidgetsApiController } from "backend/dashboard-widgets/dashboard-widgets.controller"; import { requestHandler } from "backend/lib/request"; import { IAccountProfile } from "shared/types/user"; import { USER_PERMISSIONS } from "shared/constants/user"; +import { dashboardWidgetsApiService } from "backend/dashboard-widgets/dashboard-widgets.service"; export default requestHandler( { @@ -10,7 +10,7 @@ export default requestHandler( "authenticatedUser", { _type: "requestBody", options: {} }, ]); - return await dashboardWidgetsApiController.runScript( + return await dashboardWidgetsApiService.runScript( validatedRequest.requestBody.script, validatedRequest.authenticatedUser as IAccountProfile, validatedRequest.requestBody.relativeDate @@ -21,7 +21,7 @@ export default requestHandler( "authenticatedUser", { _type: "requestQueries", options: ["widgetId", "relativeDate"] }, ]); - return await dashboardWidgetsApiController.runWidgetScript( + return await dashboardWidgetsApiService.runWidgetScript( validatedRequest.requestQueries.widgetId, validatedRequest.authenticatedUser as IAccountProfile, validatedRequest.requestQueries.relativeDate @@ -29,6 +29,10 @@ export default requestHandler( }, }, [ + { + _type: "notAllowedOnDemo", + method: ["POST"], + }, { method: ["POST"], _type: "canUser", diff --git a/src/pages/api/entities/[entity]/relations.ts b/src/pages/api/entities/[entity]/relations.ts index f560f6c7f..ec7f6d6db 100644 --- a/src/pages/api/entities/[entity]/relations.ts +++ b/src/pages/api/entities/[entity]/relations.ts @@ -1,4 +1,4 @@ -import { entitiesApiController } from "backend/entities/entities.controller"; +import { entitiesApiService } from "backend/entities/entities.service"; import { requestHandler } from "backend/lib/request"; import { IAccountProfile } from "shared/types/user"; @@ -9,7 +9,7 @@ export default requestHandler({ "authenticatedUser", ]); - return await entitiesApiController.getEntityRelations( + return await entitiesApiService.getEntityRelationsForUserRole( validatedRequest.entity, (validatedRequest.authenticatedUser as IAccountProfile).role ); diff --git a/src/pages/api/entities/active.ts b/src/pages/api/entities/active.ts index 44f12cdfd..df9ce257b 100644 --- a/src/pages/api/entities/active.ts +++ b/src/pages/api/entities/active.ts @@ -1,8 +1,8 @@ import { requestHandler } from "backend/lib/request"; -import { entitiesApiController } from "backend/entities/entities.controller"; +import { entitiesApiService } from "backend/entities/entities.service"; export default requestHandler({ GET: async () => { - return await entitiesApiController.getActiveEntities(); + return await entitiesApiService.getActiveEntities(); }, }); diff --git a/src/pages/api/entities/list.ts b/src/pages/api/entities/list.ts index 5bc0385c4..09b019808 100644 --- a/src/pages/api/entities/list.ts +++ b/src/pages/api/entities/list.ts @@ -1,8 +1,8 @@ import { requestHandler } from "backend/lib/request"; -import { entitiesApiController } from "backend/entities/entities.controller"; +import { entitiesApiService } from "backend/entities/entities.service"; export default requestHandler({ GET: async () => { - return await entitiesApiController.listAllEntities(); + return await entitiesApiService.getAllEntities(); }, }); diff --git a/src/pages/api/upload/index.ts b/src/pages/api/upload/index.ts index cc59abcbf..f8e8b9be2 100644 --- a/src/pages/api/upload/index.ts +++ b/src/pages/api/upload/index.ts @@ -4,35 +4,40 @@ import { BadRequestError } from "backend/lib/errors"; import { FORMIDABLE_ERRORS } from "backend/uploads/constants"; import { parseForm } from "backend/uploads/parse"; -export default requestHandler({ - POST: async (getValidatedRequest): Promise> => { - try { - if (process.env.NEXT_PUBLIC_IS_DEMO) { - throw new Error("File uploads will not work on demo site"); +export default requestHandler( + { + POST: async (getValidatedRequest): Promise> => { + try { + const { rawRequest: req } = await getValidatedRequest(["rawRequest"]); + + const { files } = await parseForm(req); + + if (files.file.length === 0) { + throw new BadRequestError("An invalid file was submitted"); + } + + const fileUrl = files.file[0].filepath; + + return { + fileUrl, + }; + } catch (error) { + if ( + [FORMIDABLE_ERRORS.biggerThanTotalMaxFileSize].includes(error.code) + ) { + throw new BadRequestError(error.message); + } + + throw error; } - - const { rawRequest: req } = await getValidatedRequest(["rawRequest"]); - - const { files } = await parseForm(req); - - if (files.file.length === 0) { - throw new BadRequestError("An invalid file was submitted"); - } - - const fileUrl = files.file[0].filepath; - - return { - fileUrl, - }; - } catch (error) { - if ([FORMIDABLE_ERRORS.biggerThanTotalMaxFileSize].includes(error.code)) { - throw new BadRequestError(error.message); - } - - throw error; - } + }, }, -}); + [ + { + _type: "notAllowedOnDemo", + }, + ] +); export const config: PageConfig = { api: {