Skip to content

Commit

Permalink
refactor: lint controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijsdeman committed Feb 26, 2025
1 parent 478f7c9 commit bdcacc7
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 142 deletions.
9 changes: 5 additions & 4 deletions src/auth/LDAPStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Role } from '../entity/Role';
import UserService from '../services/UserService';
import { Roles } from '../entity/enums/Roles';
import AppDataSource from '../database';
import { ExpressRequest } from '../types';

const isDefined = (i: string | undefined) => i !== undefined && i !== '';

Expand Down Expand Up @@ -44,16 +45,16 @@ export const updateUserInformation = async (user: User, ldapUser: any): Promise<
await new UserService().assignRoles(user, userRoles);

const identity = user.identityLdap;
// eslint-disable-next-line no-param-reassign

if (identity && !identity.overrideEmail) user.email = ldapUser.mail;

Check failure on line 49 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe assignment of an `any` value

Check failure on line 49 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe member access .mail on an `any` value
// eslint-disable-next-line no-param-reassign

user.firstName = ldapUser.givenName;

Check failure on line 51 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe assignment of an `any` value

Check failure on line 51 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe member access .givenName on an `any` value
// eslint-disable-next-line no-param-reassign

user.lastName = ldapUser.sn;

Check failure on line 53 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe assignment of an `any` value

Check failure on line 53 in src/auth/LDAPStrategy.ts

View workflow job for this annotation

GitHub Actions / build-and-lint / yarn-steps

Unsafe member access .sn on an `any` value
return user.save();
};

export const ldapLogin = (req: express.Request, res: express.Response, next: express.NextFunction) => {
export const ldapLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => {
passport.authenticate('ldapauth', async (err: any, ldapUser: any, info: any) => {
if (err) {
return next(err);
Expand Down
3 changes: 2 additions & 1 deletion src/auth/LocalStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IdentityLocal } from '../entity/IdentityLocal';
import { User } from '../entity/User';
import { ApiError, HTTPStatus } from '../helpers/error';
import AppDataSource from '../database';
import { ExpressRequest } from '../types';

const INVALID_LOGIN = 'Invalid email or password.';
const VERIFY_ACCOUNT = 'Please verify your account and set your password with the link received by email.';
Expand Down Expand Up @@ -67,7 +68,7 @@ export default new LocalStrategy(
},
);

export const localLogin = (req: express.Request, res: express.Response, next: express.NextFunction) => {
export const localLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => {
passport.authenticate('local', (err: any, user: any) => {
if (err) {
return next(err);
Expand Down
6 changes: 3 additions & 3 deletions src/auth/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from 'express';
import { IdentityApiKey } from '../entity/IdentityApiKey';
import { User } from '../entity/User';
import { ApiError, HTTPStatus } from '../helpers/error';
import AppDataSource from '../database';
import { ExpressRequest } from '../types';

async function authWithApiKey(apiKey: string) {
const split = apiKey.split(' ');
Expand All @@ -22,10 +22,10 @@ async function authWithApiKey(apiKey: string) {
}

export async function expressAuthentication(
request: express.Request,
request: ExpressRequest,
securityName: string,
scopes?: string[],
): Promise<any> {
): Promise<unknown> {
switch (securityName) {
case 'local': {
const auth = request.header('Authentication');
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/CompanyController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express from 'express';
import fs from 'fs';
import { body } from 'express-validator';
import { Company } from '../entity/Company';
import { Invoice } from '../entity/Invoice';
Expand All @@ -24,13 +24,14 @@ import BaseFile from '../entity/file/BaseFile';
import { CompanyFile } from '../entity/file/CompanyFile';
import StatisticsService, { ContractedProductsAnalysis } from '../services/StatisticsService';
import { Roles } from '../entity/enums/Roles';
import { ExpressRequest } from '../types';
import { ListParams } from './ListParams';
import { Body, Tags, Controller, Post, Route, Put, Get, Security, Response, Delete, Request } from 'tsoa';

@Route('company')
@Tags('Company')
export class CompanyController extends Controller {
private async validateCompanyParams(req: express.Request): Promise<void> {
private async validateCompanyParams(req: ExpressRequest): Promise<void> {
await validate(
[
body('name').notEmpty().trim(),
Expand Down Expand Up @@ -103,7 +104,7 @@ export class CompanyController extends Controller {
@Post()
@Security('local', ['ADMIN'])
@Response<WrappedApiError>(401)
public async createCompany(@Body() params: CompanyParams, @Request() req: express.Request): Promise<Company> {
public async createCompany(@Body() params: CompanyParams, @Request() req: ExpressRequest): Promise<Company> {
await this.validateCompanyParams(req);
return new CompanyService().createCompany(params);
}
Expand All @@ -120,7 +121,7 @@ export class CompanyController extends Controller {
public async updateCompany(
id: number,
@Body() params: Partial<CompanyParams>,
@Request() req: express.Request,
@Request() req: ExpressRequest,
): Promise<Company> {
await this.validateCompanyParams(req);
return new CompanyService({ actor: req.user as User }).updateCompany(id, params);
Expand All @@ -129,7 +130,6 @@ export class CompanyController extends Controller {
/**
* Delete company
* @param id ID of the company to delete
* @param req Express.js request object
*/
@Delete('{id}')
@Security('local', ['ADMIN'])
Expand All @@ -146,7 +146,7 @@ export class CompanyController extends Controller {
@Put('{id}/logo')
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async uploadCompanyLogo(@Request() req: express.Request, id: number) {
public async uploadCompanyLogo(@Request() req: ExpressRequest, id: number) {
await FileService.uploadCompanyLogo(req, id);
}

Expand Down Expand Up @@ -198,7 +198,7 @@ export class CompanyController extends Controller {
@Post('{id}/file/upload')
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async uploadCompanyFile(id: number, @Request() req: express.Request): Promise<CompanyFile> {
public async uploadCompanyFile(id: number, @Request() req: ExpressRequest): Promise<CompanyFile> {
const actor = req.user as User;
if (req.body.createdAt !== undefined && !actor.hasRole(Roles.ADMIN)) {
throw new ApiError(
Expand All @@ -207,7 +207,7 @@ export class CompanyController extends Controller {
);
}

return new FileService(CompanyFile, { actor: req.user as User }).uploadFile(req, id);
return (await new FileService(CompanyFile, { actor: req.user as User }).uploadFile(req, id)) as CompanyFile;
}

/**
Expand All @@ -219,7 +219,7 @@ export class CompanyController extends Controller {
@Get('{id}/file/{fileId}')
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async getCompanyFile(id: number, fileId: number): Promise<any> {
public async getCompanyFile(id: number, fileId: number): Promise<fs.ReadStream> {
const file = <CompanyFile>await new FileService(CompanyFile).getFile(id, fileId);

return FileHelper.putFileInResponse(this, file);
Expand All @@ -239,7 +239,7 @@ export class CompanyController extends Controller {
id: number,
fileId: number,
@Body() params: Partial<FileParams>,
@Request() req: express.Request,
@Request() req: ExpressRequest,
): Promise<BaseFile> {
await validateFileParams(req);
return new FileService(CompanyFile).updateFile(id, fileId, params);
Expand Down Expand Up @@ -269,7 +269,7 @@ export class CompanyController extends Controller {
public async addCompanyComment(
id: number,
@Body() params: ActivityParams,
@Request() req: express.Request,
@Request() req: ExpressRequest,
): Promise<BaseActivity> {
await validateCommentParams(req);
const p: FullActivityParams = {
Expand All @@ -294,7 +294,7 @@ export class CompanyController extends Controller {
id: number,
activityId: number,
@Body() params: Partial<ActivityParams>,
@Request() req: express.Request,
@Request() req: ExpressRequest,
): Promise<BaseActivity> {
await validateActivityParams(req);
const p: Partial<FullActivityParams> = {
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/ContactController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { body } from 'express-validator';
import express from 'express';
import { Contact } from '../entity/Contact';
import { WrappedApiError } from '../helpers/error';
import ContactService, { ContactListResponse, ContactParams, ContactSummary } from '../services/ContactService';
import { validate } from '../helpers/validation';
import { Gender } from '../entity/enums/Gender';
import { ContactFunction } from '../entity/enums/ContactFunction';
import { ExpressRequest } from '../types';
import { ListParams } from './ListParams';
import { Body, Controller, Post, Route, Put, Tags, Get, Security, Response, Request, Delete } from 'tsoa';

@Route('contact')
@Tags('Contact')
export class ContactController extends Controller {
private async validateContactParams(req: express.Request) {
private async validateContactParams(req: ExpressRequest) {
const emailOptionalFunctions = [
ContactFunction.SIGNATORY_AUTHORIZED,
ContactFunction.ASSISTING,
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ContactController extends Controller {
@Post()
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async createContact(@Body() params: ContactParams, @Request() req: express.Request): Promise<Contact> {
public async createContact(@Body() params: ContactParams, @Request() req: ExpressRequest): Promise<Contact> {
await this.validateContactParams(req);
return new ContactService().createContact(params);
}
Expand All @@ -89,15 +89,15 @@ export class ContactController extends Controller {
* updateContact() - update contact
* @param id ID of contact to update
* @param params Update subset of parameter of contact
* @param req: express.Request
* @param req: ExpressRequest
*/
@Put('{id}')
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async updateContact(
id: number,
@Body() params: Partial<ContactParams>,
@Request() req: express.Request,
@Request() req: ExpressRequest,
): Promise<Contact> {
await this.validateContactParams(req);
return new ContactService().updateContact(id, params);
Expand Down
Loading

0 comments on commit bdcacc7

Please sign in to comment.