Skip to content

Commit

Permalink
refactor: last lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijsdeman committed Feb 26, 2025
1 parent bdcacc7 commit 08d0605
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"gen-client": "nswag openapi2tsclient /input:src/public/swagger.json /output:../parelpracht-client/src/clients/server.generated.ts /ServiceHost:.",
"db:diagram": "typeorm-uml ormconfig.json",
"db:validate": "ts-node src/dbvalidator/validate.ts",
"lint": "eslint src",
"lint": "yarn tsoa && eslint src",
"lint:fix": "eslint . --fix",
"format": "prettier --ignore-path .gitignore --check .",
"format:fix": "prettier --ignore-path .gitignore --write ."
Expand Down
26 changes: 20 additions & 6 deletions src/auth/LDAPStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import { ExpressRequest } from '../types';

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

export interface LDAPUser {
sAMAccountName: string;
memberOfFlattened: string[];
memberOf: string[];
mail: string;
givenName: string;
sn: string;
}

interface AuthInfo {
message: string;
}

export const ldapEnabled = () =>
isDefined(process.env.LDAP_URL) &&
isDefined(process.env.LDAP_BINDDN) &&
Expand All @@ -29,7 +42,7 @@ export const LDAPStrategy = new Strategy({
},
});

const checkAllowedRoles = async (ldapUser: any): Promise<Roles[]> => {
const checkAllowedRoles = async (ldapUser: LDAPUser): Promise<Roles[]> => {
const roles = await AppDataSource.getRepository(Role).find();
const userRoles: Roles[] = [];
roles.forEach((role) => {
Expand All @@ -40,7 +53,7 @@ const checkAllowedRoles = async (ldapUser: any): Promise<Roles[]> => {
return userRoles;
};

export const updateUserInformation = async (user: User, ldapUser: any): Promise<User> => {
export const updateUserInformation = async (user: User, ldapUser: LDAPUser): Promise<User> => {
const userRoles = await checkAllowedRoles(ldapUser);
await new UserService().assignRoles(user, userRoles);

Expand All @@ -55,7 +68,8 @@ export const updateUserInformation = async (user: User, ldapUser: any): Promise<
};

export const ldapLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => {
passport.authenticate('ldapauth', async (err: any, ldapUser: any, info: any) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call -- this seems to be correct
passport.authenticate('ldapauth', async (err: Error, ldapUser: LDAPUser, info: AuthInfo) => {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -85,13 +99,13 @@ export const ldapLogin = (req: ExpressRequest, res: express.Response, next: expr
lastName: ldapUser.sn,
email: ldapUser.mail,
function: '',
} as any as User;
} as User;
user = await userRepo.save(user);

identity = {
id: user.id,
username: ldapUser.sAMAccountName,
} as any as IdentityLDAP;
} as IdentityLDAP;
identity = await identityRepo.save(identity);
identity = await identityRepo.findOne({
where: { id: identity.id },
Expand All @@ -105,7 +119,7 @@ export const ldapLogin = (req: ExpressRequest, res: express.Response, next: expr

await updateUserInformation(identity.user, ldapUser);

return req.logIn(identity.user, (e: any) => {
return req.logIn(identity.user, (e: Error) => {
// When the user enabled "remember me", we give the session cookie an
// expiration date of 30 days
if (req.body.rememberMe === true) {
Expand Down
6 changes: 4 additions & 2 deletions src/auth/LocalStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default new LocalStrategy(
usernameField: 'email',
passwordField: 'password',
},
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- async is allowed here
async (email, password, done) => {
const userRepo = AppDataSource.getRepository(User);
const identityRepo = AppDataSource.getRepository(IdentityLocal);
Expand Down Expand Up @@ -69,14 +70,15 @@ export default new LocalStrategy(
);

export const localLogin = (req: ExpressRequest, res: express.Response, next: express.NextFunction) => {
passport.authenticate('local', (err: any, user: any) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call -- this seems to be correct
passport.authenticate('local', (err: Error, user: User) => {
if (err) {
return next(err);
}
if (!user) {
return next(new ApiError(HTTPStatus.BadRequest, INVALID_LOGIN));
}
return req.logIn(user, (e: any) => {
return req.logIn(user, (e: Error) => {
// When the user enabled "remember me", we give the session cookie an
// expiration date of 30 days
if (req.body.rememberMe === true) {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/CompanyController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { Readable } from 'stream';
import { body } from 'express-validator';
import { Company } from '../entity/Company';
import { Invoice } from '../entity/Invoice';
Expand Down Expand Up @@ -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<fs.ReadStream> {
public async getCompanyFile(id: number, fileId: number): Promise<Readable> {
const file = <CompanyFile>await new FileService(CompanyFile).getFile(id, fileId);

return FileHelper.putFileInResponse(this, file);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/ContractController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { Readable } from 'stream';
import { body } from 'express-validator';
import { Contract } from '../entity/Contract';
import ContractService, { ContractListResponse, ContractParams } from '../services/ContractService';
Expand Down Expand Up @@ -323,7 +323,7 @@ export class ContractController extends Controller {
id: number,
@Body() params: GenerateContractParams,
@Request() req: ExpressRequest,
): Promise<fs.ReadStream> {
): Promise<Readable> {
await validate(
[
body('language').isIn(Object.values(Language)),
Expand Down Expand Up @@ -374,7 +374,7 @@ export class ContractController extends Controller {
@Get('{id}/file/{fileId}')
@Security('local', ['SIGNEE', 'FINANCIAL', 'GENERAL', 'ADMIN', 'AUDIT'])
@Response<WrappedApiError>(401)
public async getContractFile(id: number, fileId: number): Promise<fs.ReadStream> {
public async getContractFile(id: number, fileId: number): Promise<Readable> {
const file = <ContractFile>await new FileService(ContractFile).getFile(id, fileId);

return FileHelper.putFileInResponse(this, file);
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/InvoiceController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { Readable } from 'stream';
import { body, ValidationChain } from 'express-validator';
import { Invoice } from '../entity/Invoice';
import { ApiError, HTTPStatus, WrappedApiError } from '../helpers/error';
Expand Down Expand Up @@ -180,7 +180,7 @@ export class InvoiceController extends Controller {
id: number,
@Body() params: GenerateInvoiceParams,
@Request() req: ExpressRequest,
): Promise<fs.ReadStream> {
): Promise<Readable> {
await validate(
[
body('language').isIn(Object.values(Language)),
Expand Down Expand Up @@ -228,7 +228,7 @@ export class InvoiceController extends Controller {
@Get('{id}/file/{fileId}')
@Security('local', ['SIGNEE', 'FINANCIAL', 'GENERAL', 'ADMIN', 'AUDIT'])
@Response<WrappedApiError>(401)
public async getInvoiceFile(id: number, fileId: number): Promise<fs.ReadStream> {
public async getInvoiceFile(id: number, fileId: number): Promise<Readable> {
const file = <InvoiceFile>await new FileService(InvoiceFile).getFile(id, fileId);

return FileHelper.putFileInResponse(this, file);
Expand Down Expand Up @@ -278,7 +278,7 @@ export class InvoiceController extends Controller {
public async generateCustomInvoice(
@Body() params: CustomInvoiceGenSettings,
@Request() req: ExpressRequest,
): Promise<fs.ReadStream> {
): Promise<Readable> {
await validate(
[
body('language').isIn(Object.values(Language)),
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export type SortDirection = 'ASC' | 'DESC';

export interface ListOrFilter {
column: string;
values: unknown[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO check if change required after rewriting queries
values: any[];
}
4 changes: 2 additions & 2 deletions src/controllers/ProductController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { Readable } from 'stream';
import { body } from 'express-validator';
import { Product } from '../entity/Product';
import ProductService, {
Expand Down Expand Up @@ -230,7 +230,7 @@ export class ProductController extends Controller {
@Get('{id}/file/{fileId}')
@Security('local', ['GENERAL', 'ADMIN'])
@Response<WrappedApiError>(401)
public async getProductFile(id: number, fileId: number): Promise<fs.ReadStream> {
public async getProductFile(id: number, fileId: number): Promise<Readable> {
const file = <ProductFile>await new FileService(ProductFile).getFile(id, fileId);

return FileHelper.putFileInResponse(this, file);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/fileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import path from 'path';
import { Readable } from 'stream';
import mime from 'mime';
import BaseFile from '../entity/file/BaseFile';
import { Controller } from 'tsoa';
Expand All @@ -18,7 +19,7 @@ export default class FileHelper {
* @param controller Controller that handles the request
* @param file File to add to the response
*/
public static putFileInResponse(controller: Controller, file: BaseFile): fs.ReadStream {
public static putFileInResponse(controller: Controller, file: BaseFile): Readable {
const stat = fs.statSync(file.location);

controller.setStatus(200);
Expand Down
5 changes: 3 additions & 2 deletions src/timedevents/events/ldapGroups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient, SearchCallbackResponse, SearchEntry } from 'ldapjs';
import { IdentityLDAP } from '../../entity/IdentityLDAP';
import { updateUserInformation } from '../../auth';
import { LDAPUser, updateUserInformation } from '../../auth';
import AppDataSource from '../../database';

export default async function ldapGroups() {
Expand Down Expand Up @@ -28,7 +28,8 @@ export default async function ldapGroups() {
}

res.on('searchEntry', (entry: SearchEntry) => {
updateUserInformation(identity.user, entry.object).catch((err) => console.error(err));
// TODO check if there is a better way to cast this type
updateUserInformation(identity.user, entry.object as unknown as LDAPUser).catch((err) => console.error(err));
});
},
);
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

/* Advanced Options */
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"ts-node": {
"files": true
},
"include": ["src", "src/**/*.json", "build", "build/**/*.json"],
"include": ["src", "src/**/*.json", "build/**/*.ts", "build/**/*.json"],
"exclude": ["node_modules", "dist"]
}
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1608,13 +1608,13 @@ __metadata:
linkType: hard

"axios@npm:*, axios@npm:^1.7.9":
version: 1.7.9
resolution: "axios@npm:1.7.9"
version: 1.8.1
resolution: "axios@npm:1.8.1"
dependencies:
follow-redirects: "npm:^1.15.6"
form-data: "npm:^4.0.0"
proxy-from-env: "npm:^1.1.0"
checksum: 10c0/b7a41e24b59fee5f0f26c1fc844b45b17442832eb3a0fb42dd4f1430eb4abc571fe168e67913e8a1d91c993232bd1d1ab03e20e4d1fee8c6147649b576fc1b0b
checksum: 10c0/b2e1d5a61264502deee4b50f0a6df0aa3b174c546ccf68c0dff714a2b8863232e0bd8cb5b84f853303e97f242a98260f9bb9beabeafe451ad5af538e9eb7ac22
languageName: node
linkType: hard

Expand Down Expand Up @@ -2465,13 +2465,13 @@ __metadata:
linkType: hard

"eslint-config-prettier@npm:^10.0.1":
version: 10.0.1
resolution: "eslint-config-prettier@npm:10.0.1"
version: 10.0.2
resolution: "eslint-config-prettier@npm:10.0.2"
peerDependencies:
eslint: ">=7.0.0"
bin:
eslint-config-prettier: build/bin/cli.js
checksum: 10c0/e2434931669d211663c0493f2c1640a670a02ba4503a68f056a7eda133f383acbbb983a4a7bd0ad6cb3b2bc4d5731c3be8b32fe28e35087a76fea45f7061ae70
checksum: 10c0/e0ef3c442661a26fc6e82acec5bb9a418c4a8f65ec8adf0983d3aaba7716d2ed448358b063cce6e3c272c847d14cb856ddf30031770c6571e2b2c3e2a439afd4
languageName: node
linkType: hard

Expand Down Expand Up @@ -2839,11 +2839,11 @@ __metadata:
linkType: hard

"fastq@npm:^1.6.0":
version: 1.19.0
resolution: "fastq@npm:1.19.0"
version: 1.19.1
resolution: "fastq@npm:1.19.1"
dependencies:
reusify: "npm:^1.0.4"
checksum: 10c0/d6a001638f1574a696660fcbba5300d017760432372c801632c325ca7c16819604841c92fd3ccadcdacec0966ca336363a5ff57bc5f0be335d8ea7ac6087b98f
checksum: 10c0/ebc6e50ac7048daaeb8e64522a1ea7a26e92b3cee5cd1c7f2316cdca81ba543aa40a136b53891446ea5c3a67ec215fbaca87ad405f102dd97012f62916905630
languageName: node
linkType: hard

Expand Down Expand Up @@ -5449,9 +5449,9 @@ __metadata:
linkType: hard

"reusify@npm:^1.0.4":
version: 1.0.4
resolution: "reusify@npm:1.0.4"
checksum: 10c0/c19ef26e4e188f408922c46f7ff480d38e8dfc55d448310dfb518736b23ed2c4f547fb64a6ed5bdba92cd7e7ddc889d36ff78f794816d5e71498d645ef476107
version: 1.1.0
resolution: "reusify@npm:1.1.0"
checksum: 10c0/4eff0d4a5f9383566c7d7ec437b671cc51b25963bd61bf127c3f3d3f68e44a026d99b8d2f1ad344afff8d278a8fe70a8ea092650a716d22287e8bef7126bb2fa
languageName: node
linkType: hard

Expand Down

0 comments on commit 08d0605

Please sign in to comment.