Skip to content

Commit ddce922

Browse files
committed
chore(review): apply coderabbit fixes
1 parent 65f68cc commit ddce922

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/v3/middleware/app_version_checker.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextFunction, Request, Response } from 'express';
22
import { check, validationResult } from 'express-validator';
3-
import { API_VAR } from '../../index.js';
43
import { formatError } from '../utils/format_log.js';
4+
import { isValidClientVersion } from '../utils/app.js';
55

66
export const appVersionChecker = () => {
77
return async (req: Request, res: Response, next: NextFunction) => {
@@ -30,20 +30,16 @@ export const appVersionChecker = () => {
3030
return;
3131
}
3232

33-
const appMinimum = API_VAR.MINIMUM_APP_VERSION;
33+
const validVersion = isValidClientVersion(appVersion);
3434

35-
const majorOK = +appVersion.split('.')[0] > +appMinimum.split('.')[0];
36-
const minorOK = +appVersion.split('.')[1] > +appMinimum.split('.')[1];
37-
const patchOK = +appVersion.split('.')[2] > +appMinimum.split('.')[2];
38-
39-
if (appMinimum === appVersion || majorOK || (!majorOK && minorOK) || (!minorOK && patchOK)) {
40-
next();
35+
if (!validVersion) {
36+
res.locals.type = 'warn';
37+
res.locals.message = `client version outdated`;
38+
res.status(400).json({ message: 'CLIENT_VERSION_OUTDATED' });
4139
return;
4240
}
4341

44-
res.locals.type = 'warn';
45-
res.locals.message = `client version outdated`;
46-
res.status(400).json({ message: 'CLIENT_VERSION_OUTDATED' });
42+
next();
4743
} catch (err) {
4844
next(err);
4945
}

src/v3/routes/admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ router.get('/logout', logoutAdmin);
5050
router.get('/client-version', getClientVersion);
5151

5252
// get minimum client
53-
router.post('/client-version', updateClientVersion);
53+
router.post('/client-version', body('version').isString().notEmpty(), updateClientVersion);
5454

5555
// create new congregation
5656
router.post(

src/v3/utils/app.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CookieOptions, Request } from 'express';
2+
import { API_VAR } from '../../index.js';
23

34
const isLocalRequest = (req: Request) => {
45
const host = req.get('host');
@@ -16,3 +17,19 @@ export const cookieOptions = (req: Request): CookieOptions => {
1617
maxAge: 400 * 24 * 60 * 60 * 1000,
1718
};
1819
};
20+
21+
export const isValidClientVersion = (version: string) => {
22+
const parts1 = version.split('.').map(Number);
23+
const parts2 = API_VAR.MINIMUM_APP_VERSION.split('.').map(Number);
24+
25+
const maxLength = Math.max(parts1.length, parts2.length);
26+
for (let i = 0; i < maxLength; i++) {
27+
const num1 = parts1[i] ?? 0;
28+
const num2 = parts2[i] ?? 0;
29+
30+
if (num1 > num2) return true;
31+
if (num1 < num2) return false;
32+
}
33+
34+
return true;
35+
};

0 commit comments

Comments
 (0)