Skip to content

Commit

Permalink
♻️ refactor(validations): centralize no allowed on demo validations
Browse files Browse the repository at this point in the history
  • Loading branch information
thrownullexception committed Jan 4, 2024
1 parent 0b63397 commit feea583
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 218 deletions.
83 changes: 0 additions & 83 deletions src/backend/dashboard-widgets/dashboard-widgets.controller.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/backend/entities/entities.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ILabelValue } from "shared/types/options";
import { IEntityRelation } from "shared/types/db";
import {
ConfigurationApiService,
configurationApiService,
Expand All @@ -12,14 +10,6 @@ export class EntitiesApiController {
private _configurationApiService: ConfigurationApiService
) {}

async getActiveEntities(): Promise<ILabelValue[]> {
return await this._entitiesApiService.getActiveEntities();
}

async listAllEntities(): Promise<ILabelValue[]> {
return await this._entitiesApiService.getAllEntities();
}

async listAllEntityRelations(entity: string): Promise<string[]> {
const [entityRelations, disabledEntities] = await Promise.all([
this._entitiesApiService.getEntityRelations(entity),
Expand All @@ -32,16 +22,6 @@ export class EntitiesApiController {

return allowedEntityRelation.map(({ table }) => table);
}

async getEntityRelations(
entity: string,
userRole: string
): Promise<IEntityRelation[]> {
return await this._entitiesApiService.getEntityRelationsForUserRole(
entity,
userRole
);
}
}

export const entitiesApiController = new EntitiesApiController(
Expand Down
2 changes: 2 additions & 0 deletions src/backend/lib/request/validations/implementations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -31,6 +32,7 @@ export const ValidationImpl: Record<
requestQuery,
canUser,
rawRequest,
notAllowedOnDemo,
requestQueries,
authenticatedUser,
entity,
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
};
1 change: 1 addition & 0 deletions src/backend/lib/request/validations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ValidationKeys = {
| "rawRequest"
| "paginationFilter"
| "canUser"
| "notAllowedOnDemo"
| "crudEnabled"
| "requestBody"
| "requestQuery"
Expand Down
36 changes: 1 addition & 35 deletions src/backend/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<IAuthenticatedUserBag> {
Expand All @@ -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(
Expand Down
12 changes: 3 additions & 9 deletions src/backend/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -101,10 +102,6 @@ export class UsersApiService implements IApplicationService {
newPassword: string;
}
) {
if (process.env.NEXT_PUBLIC_IS_DEMO) {
return;
}

try {
await this.checkUserPassword({
username,
Expand All @@ -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),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function ListManager<T, K extends StringProps<T>>({
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(() => {
Expand Down
10 changes: 4 additions & 6 deletions src/pages/api/account/[username]/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) => {
Expand All @@ -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
);
Expand All @@ -44,7 +42,7 @@ export default requestHandler(
options: REQUEST_QUERY_FIELD,
},
]);
return await usersApiController.updateProfile(
return await usersApiService.updateUser(
validatedRequest.requestQuery,
validatedRequest.requestBody
);
Expand Down
7 changes: 5 additions & 2 deletions src/pages/api/account/[username]/reset-password.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -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,
Expand Down
37 changes: 22 additions & 15 deletions src/pages/api/account/change-password.ts
Original file line number Diff line number Diff line change
@@ -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",
},
]
);
6 changes: 3 additions & 3 deletions src/pages/api/account/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -19,6 +19,6 @@ export default requestHandler({
options: CREATE_USER_FORM_SCHEMA,
},
]);
return await usersApiController.createUser(validatedRequest.requestBody);
return await usersApiService.registerUser(validatedRequest.requestBody);
},
});
Loading

0 comments on commit feea583

Please sign in to comment.