Skip to content
Open
10 changes: 10 additions & 0 deletions src/backend/src/controllers/cars.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ export default class CarsController {
next(error);
}
}

static async getCurrentCar(req: Request, res: Response, next: NextFunction) {
try {
const car = await CarsService.getCurrentCar(req.organization);

res.status(200).json(car);
} catch (error: unknown) {
next(error);
}
}
}
34 changes: 34 additions & 0 deletions src/backend/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,40 @@ const performSeed: () => Promise<void> = async () => {
}
});

const car24 = await prisma.car.create({
data: {
wbsElement: {
create: {
name: 'NER-24',
carNumber: 24,
projectNumber: 0,
workPackageNumber: 0,
organizationId
}
}
},
include: {
wbsElement: true
}
});

const car25 = await prisma.car.create({
data: {
wbsElement: {
create: {
name: 'NER-25',
carNumber: 25,
projectNumber: 0,
workPackageNumber: 0,
organizationId
}
}
},
include: {
wbsElement: true
}
});

/**
* Make an initial change request for car 1 using the wbs of the genesis project
*/
Expand Down
2 changes: 2 additions & 0 deletions src/backend/src/routes/cars.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const carsRouter = express.Router();

carsRouter.get('/', CarsController.getAllCars);

carsRouter.get('/current', CarsController.getCurrentCar);

carsRouter.post('/create', CarsController.createCar);

export default carsRouter;
22 changes: 22 additions & 0 deletions src/backend/src/services/car.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,26 @@ export default class CarsService {

return carTransformer(car);
}

static async getCurrentCar(organization: Organization) {
const car = await prisma.car.findFirst({
where: {
wbsElement: {
organizationId: organization.organizationId
}
},
orderBy: {
wbsElement: {
carNumber: 'desc'
}
},
...getCarQueryArgs(organization.organizationId)
});

if (!car) {
return null;
}

return carTransformer(car);
}
}
Loading