refactor: Improve type inference and error handling in various services and tests
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Has been cancelled

This commit is contained in:
2025-12-14 19:00:28 -08:00
parent d4739b5784
commit 35c0fb3bfe
18 changed files with 99 additions and 59 deletions

View File

@@ -224,10 +224,9 @@ router.get('/unmatched-items', async (req, res, next: NextFunction) => {
* DELETE /api/admin/recipes/:recipeId - Admin endpoint to delete any recipe.
*/
router.delete('/recipes/:recipeId', validateRequest(numericIdParamSchema('recipeId')), async (req: Request, res: Response, next: NextFunction) => {
// Define schema locally to simplify type inference
const schema = numericIdParamSchema('recipeId');
const adminUser = req.user as UserProfile;
const { params } = req as unknown as z.infer<typeof schema>;
// Infer the type directly from the schema generator function.
const { params } = req as unknown as z.infer<ReturnType<typeof numericIdParamSchema>>;
try {
// The isAdmin flag bypasses the ownership check in the repository method.
await db.recipeRepo.deleteRecipe(params.recipeId, adminUser.user_id, true, req.log);
@@ -241,9 +240,8 @@ router.delete('/recipes/:recipeId', validateRequest(numericIdParamSchema('recipe
* DELETE /api/admin/flyers/:flyerId - Admin endpoint to delete a flyer and its items.
*/
router.delete('/flyers/:flyerId', validateRequest(numericIdParamSchema('flyerId')), async (req: Request, res: Response, next: NextFunction) => {
// Define schema locally to simplify type inference
const schema = numericIdParamSchema('flyerId');
const { params } = req as unknown as z.infer<typeof schema>;
// Infer the type directly from the schema generator function.
const { params } = req as unknown as z.infer<ReturnType<typeof numericIdParamSchema>>;
try {
await db.flyerRepo.deleteFlyer(params.flyerId, req.log);
res.status(204).send();

View File

@@ -1,5 +1,5 @@
// src/routes/price.routes.ts
import { Router, Request, Response, NextFunction } from 'express';
import { Router, Request, Response } from 'express';
import { z } from 'zod';
import { validateRequest } from '../middleware/validation.middleware';

View File

@@ -60,9 +60,9 @@ describe('System Routes (/api/system)', () => {
callback?: ExecCallback | null
) => {
// The actual callback can be the second or third argument.
const cb = (typeof options === 'function' ? options : callback) as ExecCallback;
if (callback) {
callback(null, pm2OnlineOutput, '');
const actualCallback = (typeof options === 'function' ? options : callback) as ExecCallback;
if (actualCallback) {
actualCallback(null, pm2OnlineOutput, '');
}
// Return a minimal object that satisfies the ChildProcess type for .unref()
return { unref: () => {} } as ReturnType<typeof exec>;
@@ -84,9 +84,9 @@ describe('System Routes (/api/system)', () => {
options?: ExecOptions | ((error: ExecException | null, stdout: string, stderr: string) => void) | null,
callback?: ((error: ExecException | null, stdout: string, stderr: string) => void) | null
) => {
const cb = (typeof options === 'function' ? options : callback) as ((error: ExecException | null, stdout: string, stderr: string) => void);
if (callback) {
callback(null, pm2StoppedOutput, '');
const actualCallback = (typeof options === 'function' ? options : callback) as ((error: ExecException | null, stdout: string, stderr: string) => void);
if (actualCallback) {
actualCallback(null, pm2StoppedOutput, '');
}
return { unref: () => {} } as ReturnType<typeof exec>;
});
@@ -104,9 +104,9 @@ describe('System Routes (/api/system)', () => {
options?: ExecOptions | ((error: ExecException | null, stdout: string, stderr: string) => void) | null,
callback?: ((error: ExecException | null, stdout: string, stderr: string) => void) | null
) => {
const cb = (typeof options === 'function' ? options : callback) as ((error: ExecException | null, stdout: string, stderr: string) => void);
if (callback) {
callback(new Error('System error') as ExecException, '', 'stderr output');
const actualCallback = (typeof options === 'function' ? options : callback) as ((error: ExecException | null, stdout: string, stderr: string) => void);
if (actualCallback) {
actualCallback(new Error('System error') as ExecException, '', 'stderr output');
}
return { unref: () => {} } as ReturnType<typeof exec>;
});