71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import * as partService from '../services/partService.js';
|
|
import { IAppUser } from '../middleware/roleMiddleware.js';
|
|
|
|
interface AuthRequest extends Request {
|
|
appUser?: IAppUser;
|
|
}
|
|
|
|
export const createPart = async (req: AuthRequest, res: Response) => {
|
|
try {
|
|
console.log('[CREATE PART] Received data:', JSON.stringify(req.body, null, 2));
|
|
const organizationId = req.appUser?.organizationId;
|
|
const part = await partService.createPart({ ...req.body, organizationId });
|
|
console.log('[CREATE PART] Success:', part);
|
|
res.status(201).json(part);
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
console.error('[CREATE PART] Error:', message);
|
|
res.status(500).json({ error: message });
|
|
}
|
|
};
|
|
|
|
export const getPartsByProject = async (req: AuthRequest, res: Response) => {
|
|
try {
|
|
const { projectId } = req.params;
|
|
const organizationId = req.appUser?.organizationId;
|
|
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
|
|
const parts = await partService.getPartsByProject(projectId as string, organizationId, isGlobalAdmin);
|
|
res.json(parts);
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
res.status(500).json({ error: message });
|
|
}
|
|
};
|
|
|
|
export const updatePart = async (req: AuthRequest, res: Response) => {
|
|
try {
|
|
const organizationId = req.appUser?.organizationId;
|
|
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
|
|
const part = await partService.updatePart(req.params.id as string, req.body, organizationId, isGlobalAdmin);
|
|
res.json(part);
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
res.status(500).json({ error: message });
|
|
}
|
|
};
|
|
|
|
export const deletePart = async (req: AuthRequest, res: Response) => {
|
|
try {
|
|
const organizationId = req.appUser?.organizationId;
|
|
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
|
|
await partService.deletePart(req.params.id as string, organizationId, isGlobalAdmin);
|
|
res.status(204).send();
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
res.status(500).json({ error: message });
|
|
}
|
|
};
|
|
|
|
export const getAllParts = async (req: AuthRequest, res: Response) => {
|
|
try {
|
|
const organizationId = req.appUser?.organizationId;
|
|
const isGlobalAdmin = req.appUser?.email === 'admtracksteel@gmail.com';
|
|
const parts = await partService.getAllParts(organizationId, isGlobalAdmin);
|
|
res.json(parts);
|
|
} catch (error: unknown) {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
res.status(500).json({ error: message });
|
|
}
|
|
};
|