Skip to content

Commit 1c36dc4

Browse files
committed
linting the backend
1 parent 747426f commit 1c36dc4

File tree

12 files changed

+109
-91
lines changed

12 files changed

+109
-91
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ jobs:
3131
run: |
3232
npm install
3333
npm test
34+
npm run lint
3435
env:
3536
CI: true

backend/.eslintrc.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports = {
3636
"max-len": [1, { "code": 140 }],
3737
"dot-notation": [1],
3838
"padded-blocks": [0],
39-
"object-curly-newline": ["error", { "ImportDeclaration": "never" }],
4039
"no-plusplus": [0],
4140
"no-console": "warn",
4241
"arrow-parens": ["error", "as-needed"],
@@ -56,8 +55,8 @@ module.exports = {
5655
"@typescript-eslint/indent": ["error", 2],
5756
"object-property-newline": [0],
5857
"object-curly-newline": ["error", {
59-
"ObjectExpression": { "multiline": true, "minProperties": 4 },
60-
"ObjectPattern": { "multiline": true, "minProperties": 4 },
58+
"ObjectExpression": { "multiline": true, "minProperties": 5 },
59+
"ObjectPattern": { "multiline": true, "minProperties": 5 },
6160
"ImportDeclaration": { "multiline": true, "minProperties": 8 },
6261
"ExportDeclaration": { "multiline": true, "minProperties": 3 }
6362
}],

backend/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as dotenv from 'dotenv';
22

33
if (process.env.NODE_ENV === 'development') {
4-
console.log('Starting with NODE_ENV=development');
4+
console.log('Starting with NODE_ENV=development'); // eslint-disable-line no-console
55
dotenv.config();
66
}
77

backend/src/controllers/attachments.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {IAttachmentCollection, IAttachmentModelConfig, attachmentModelsConfig, I
66
import {CollectionNames, SocketEventTypes} from '../models/common';
77
import {IInvoice} from '../models/invoices';
88
import {IProjectMonthOverview, TimesheetCheckAttachmentType} from '../models/projectsMonth';
9-
import { emitEntityEvent } from './utils/entity-events';
10-
import { ConfacRequest } from '../models/technical';
9+
import {emitEntityEvent} from './utils/entity-events';
10+
import {ConfacRequest} from '../models/technical';
1111

1212

1313
const saveAttachment = async (req: Request, attachmentModelConfig: IAttachmentModelConfig, file: Express.Multer.File) => {
@@ -35,7 +35,7 @@ const saveAttachment = async (req: Request, attachmentModelConfig: IAttachmentMo
3535
SocketEventTypes.EntityUpdated,
3636
standardCollectionName,
3737
result._id,
38-
result
38+
result,
3939
);
4040
}
4141

@@ -60,7 +60,8 @@ export const saveAttachmentController = async (req: Request, res: Response) => {
6060

6161
if (attachmentModelConfig.standardCollectionName === CollectionNames.ATTACHMENTS_PROJECT_MONTH_OVERVIEW) {
6262
const month = id;
63-
const inserted = await req.db.collection<IProjectMonthOverview>(CollectionNames.ATTACHMENTS_PROJECT_MONTH_OVERVIEW).findOneAndUpdate({month}, {
63+
const projectMonthOverviewCollection = req.db.collection<IProjectMonthOverview>(CollectionNames.ATTACHMENTS_PROJECT_MONTH_OVERVIEW);
64+
const inserted = await projectMonthOverviewCollection.findOneAndUpdate({month}, {
6465
$set: {
6566
[type]: file.buffer,
6667
fileDetails: {
@@ -95,17 +96,18 @@ export const saveAttachmentController = async (req: Request, res: Response) => {
9596

9697

9798

98-
99-
100-
10199
const deleteAttachment = async (id: string, type: string, req: Request, attachmentModelConfig: IAttachmentModelConfig) => {
102100
const {standardCollectionName, attachmentCollectionName} = attachmentModelConfig;
103101

104102
const data = await req.db.collection<IAttachments>(standardCollectionName).findOne({_id: new ObjectID(id)});
105103
const {_id, attachments} = data!;
106104

107105
const updatedAttachments = attachments.filter(attachment => attachment.type !== type);
108-
const inserted = await req.db.collection(standardCollectionName).findOneAndUpdate({_id}, {$set: {attachments: updatedAttachments}}, {returnOriginal: false});
106+
const inserted = await req.db.collection(standardCollectionName).findOneAndUpdate(
107+
{_id},
108+
{$set: {attachments: updatedAttachments}},
109+
{returnOriginal: false},
110+
);
109111

110112
const result = inserted.value;
111113
if (result) {
@@ -114,7 +116,7 @@ const deleteAttachment = async (id: string, type: string, req: Request, attachme
114116
SocketEventTypes.EntityUpdated,
115117
standardCollectionName,
116118
result._id,
117-
result
119+
result,
118120
);
119121
}
120122

@@ -155,14 +157,8 @@ export const deleteAttachmentController = async (req: Request, res: Response) =>
155157

156158

157159

158-
159-
160-
161-
162-
163-
164160
export const getAttachmentController = async (req: Request, res: Response) => {
165-
const { id, model, type, fileName } = req.params;
161+
const {id, model, type, fileName} = req.params;
166162
const attachmentModelConfig: IAttachmentModelConfig | undefined = attachmentModelsConfig.find(m => m.name === model);
167163

168164
if (!attachmentModelConfig) {
@@ -237,14 +233,6 @@ export const getAttachmentController = async (req: Request, res: Response) => {
237233

238234

239235

240-
241-
242-
243-
244-
245-
246-
247-
248236
export const createZipWithInvoicesController = async (req: Request, res: Response) => {
249237
const invoiceIds: ObjectID[] = req.body.map((invoiceId: string) => new ObjectID(invoiceId));
250238

backend/src/controllers/clients.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export const saveClient = async (req: ConfacRequest, res: Response) => {
2929
if (_id) {
3030
client.audit = updateAudit(client.audit, req.user);
3131
const clientsCollection = req.db.collection<IClient>(CollectionNames.CLIENTS);
32-
const {value: originalClient} = await clientsCollection.findOneAndUpdate({_id: new ObjectID(_id)}, {$set: client}, {returnOriginal: true});
32+
const {value: originalClient} = await clientsCollection.findOneAndUpdate(
33+
{_id: new ObjectID(_id)},
34+
{$set: client},
35+
{returnOriginal: true},
36+
);
3337

3438
await saveAudit(req, 'client', originalClient, client);
3539
const clientResponse = {_id, ...client};

backend/src/controllers/config.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {getTemplatesPath} from './utils';
77
import {CollectionNames, SocketEventTypes, updateAudit} from '../models/common';
88
import {ConfacRequest} from '../models/technical';
99
import {saveAudit} from './utils/audit-logs';
10-
import { emitEntityEvent } from './utils/entity-events';
10+
import {emitEntityEvent} from './utils/entity-events';
1111

1212
export const getCompanyConfig = async (req: Request, res: Response) => {
1313
const companyConfig: ICompanyConfig | null = await req.db.collection(CollectionNames.CONFIG).findOne({key: 'conf'});
@@ -21,14 +21,12 @@ export const getCompanyConfig = async (req: Request, res: Response) => {
2121

2222

2323
/** Unprotected route */
24-
export const getSecurityConfig = async (req: Request, res: Response) => {
25-
return res.send({
26-
googleClientId: appConfig.security.clientId,
27-
jwtInterval: Math.floor(appConfig.jwt.expiresIn / 2),
28-
env: appConfig.ENVIRONMENT,
29-
tag: appConfig.tag,
30-
});
31-
};
24+
export const getSecurityConfig = async (req: Request, res: Response) => res.send({
25+
googleClientId: appConfig.security.clientId,
26+
jwtInterval: Math.floor(appConfig.jwt.expiresIn / 2),
27+
env: appConfig.ENVIRONMENT,
28+
tag: appConfig.tag,
29+
});
3230

3331

3432

@@ -63,7 +61,7 @@ export const getTemplates = (req: Request, res: Response) => {
6361

6462

6563
/** Get logs_audit for an entity */
66-
export const getAudit = async (req: Request<void, void, void, {model: string, modelId: number}>, res: Response) => {
64+
export const getAudit = async (req: Request<void, void, void, {model: string; modelId: number}>, res: Response) => {
6765
const logs = await req.db.collection('logs_audit')
6866
.find({model: req.query.model, modelId: new ObjectID(req.query.modelId)})
6967
.toArray();

backend/src/controllers/emailInvoices.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import {Request, Response} from 'express';
22
import PDFMerge from 'pdf-merge';
33
import sgMail from '@sendgrid/mail';
4-
import {MailData} from '@sendgrid/helpers/classes/mail';
4+
import {MailData} from '@sendgrid/helpers/classes/mail'; // eslint-disable-line import/no-extraneous-dependencies
55
import fs from 'fs';
66
import tmp from 'tmp';
77
import {ObjectID} from 'mongodb';
88
import {IAttachmentCollection, ISendGridAttachment} from '../models/attachments';
99
import {IEmail} from '../models/clients';
1010
import {CollectionNames, IAttachment, SocketEventTypes} from '../models/common';
11-
import { emitEntityEvent } from './utils/entity-events';
12-
import { ConfacRequest } from '../models/technical';
11+
import {emitEntityEvent} from './utils/entity-events';
12+
import {ConfacRequest} from '../models/technical';
1313

1414

1515
type EmailAttachmentRequest = {
@@ -24,28 +24,31 @@ export const emailInvoiceController = async (req: Request<{id: number}, any, Ema
2424
const invoiceId = req.params.id;
2525
const email = req.body;
2626

27-
const attachmentTypes = email.attachments.map(a => a.type).reduce((acc: { [key: string]: number; }, cur) => {
27+
const attachmentTypes = email.attachments.map(a => a.type).reduce((acc: {[key: string]: number}, cur) => {
2828
acc[cur] = 1;
2929
return acc;
3030
}, {});
3131
const attachmentBuffers: IAttachmentCollection | null = await req.db
3232
.collection(CollectionNames.ATTACHMENTS)
3333
.findOne({_id: new ObjectID(invoiceId)}, attachmentTypes);
3434

35-
if (!attachmentBuffers)
36-
return res.status(500).send({message: "Couldn't find attachments!?"});
35+
if (!attachmentBuffers) {
36+
return res.status(500).send({message: 'Couldn\'t find attachments!?'});
37+
}
3738

38-
if (email.combineAttachments && email.attachments.some(attachment => attachment.fileType !== 'application/pdf'))
39+
if (email.combineAttachments && email.attachments.some(attachment => attachment.fileType !== 'application/pdf')) {
3940
return res.status(400).send({message: 'Emailing with combineAttachments=true: Can only merge pdfs'});
41+
}
4042

4143

4244
const data = await req.db.collection('attachments_config').findOne({});
4345
const termsAndConditions: Buffer = data?.TermsAndConditions?.buffer;
4446

4547
const mailData = await buildInvoiceEmailData(email, attachmentBuffers, termsAndConditions);
4648
const emailRes = await sendEmail(res, mailData);
47-
if (emailRes)
49+
if (emailRes) {
4850
return emailRes;
51+
}
4952

5053
if (req.query.emailInvoiceOnly) {
5154
await sendInvoiceOnlyEmail(email, attachmentBuffers, req.query.emailInvoiceOnly);
@@ -62,7 +65,7 @@ export const emailInvoiceController = async (req: Request<{id: number}, any, Ema
6265
SocketEventTypes.EntityUpdated,
6366
CollectionNames.INVOICES,
6467
updatedInvoice.value._id,
65-
{...updatedInvoice.value, lastEmail: lastEmailSent}
68+
{...updatedInvoice.value, lastEmail: lastEmailSent},
6669
);
6770
}
6871

@@ -74,7 +77,7 @@ export const emailInvoiceController = async (req: Request<{id: number}, any, Ema
7477
async function buildInvoiceEmailData(
7578
email: EmailRequest,
7679
attachmentBuffers: IAttachmentCollection,
77-
termsAndConditions: Buffer
80+
termsAndConditions: Buffer,
7881
): Promise<MailData> {
7982

8083
// Make sure the invoice is the first document in the array
@@ -125,6 +128,7 @@ async function buildInvoiceEmailData(
125128
fs.writeSync(termsCondFile.fd, termsAndConditions);
126129

127130
const mergedInvoicePdf: Buffer = await PDFMerge([invoiceFile, termsCondFile].map(f => f.name));
131+
// eslint-disable-next-line no-confusing-arrow
128132
sendGridAttachments = sendGridAttachments.map((att, idx) => idx === 0 ? {...att, content: mergedInvoicePdf} : att);
129133

130134
invoiceFile.removeCallback();
@@ -178,8 +182,13 @@ async function sendEmail(res: Response, mailData: MailData): Promise<Response |
178182
/**
179183
* Send email with only the invoice, not the timesheet etc
180184
* This email is sent only once
181-
**/
182-
async function sendInvoiceOnlyEmail(email: EmailRequest, attachmentBuffers: IAttachmentCollection, emailInvoiceOnly: string): Promise<void> {
185+
* */
186+
async function sendInvoiceOnlyEmail(
187+
email: EmailRequest,
188+
attachmentBuffers: IAttachmentCollection,
189+
emailInvoiceOnly: string,
190+
): Promise<void> {
191+
183192
const attachment = email.attachments.find(x => x.type === 'pdf')!;
184193
const invoiceOnlyAttachments = [{
185194
content: attachmentBuffers[attachment.type].toString('base64'),
@@ -197,6 +206,6 @@ async function sendInvoiceOnlyEmail(email: EmailRequest, attachmentBuffers: IAtt
197206
};
198207

199208
await sgMail.send(invoiceOnlyData, false).then(() => {
200-
console.log(`emailInvoiceOnly sent to ${emailInvoiceOnly}`);
209+
console.log(`emailInvoiceOnly sent to ${emailInvoiceOnly}`); // eslint-disable-line no-console
201210
});
202211
}

0 commit comments

Comments
 (0)