66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
'use server';
|
|
|
|
import { IUnits } from '@/app/_utils/types/units';
|
|
import { getInjection } from '@/di/container';
|
|
import db from '@/prisma/db';
|
|
import { AuthenticationError } from '@/src/entities/errors/auth';
|
|
import { InputParseError } from '@/src/entities/errors/common';
|
|
|
|
export async function getUnits(): Promise<IUnits[]> {
|
|
const instrumentationService = getInjection('IInstrumentationService');
|
|
return await instrumentationService.instrumentServerAction(
|
|
'District Crime Data',
|
|
{ recordResponse: true },
|
|
async () => {
|
|
try {
|
|
const units = await db.units.findMany({
|
|
include: {
|
|
districts: {
|
|
select: {
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return units
|
|
.filter((unit) => unit.district_id !== null)
|
|
.map((unit) => ({
|
|
...unit,
|
|
district_id: unit.district_id as string,
|
|
district_name: unit.districts?.name ?? '',
|
|
}));
|
|
|
|
} catch (err) {
|
|
if (err instanceof InputParseError) {
|
|
// return {
|
|
// error: err.message,
|
|
// };
|
|
|
|
throw new InputParseError(err.message);
|
|
}
|
|
|
|
if (err instanceof AuthenticationError) {
|
|
// return {
|
|
// error: 'User not found.',
|
|
// };
|
|
|
|
throw new AuthenticationError(
|
|
'There was an error with the credentials. Please try again or contact support.'
|
|
);
|
|
}
|
|
|
|
const crashReporterService = getInjection('ICrashReporterService');
|
|
crashReporterService.report(err);
|
|
// return {
|
|
// error:
|
|
// 'An error happened. The developers have been notified. Please try again later.',
|
|
// };
|
|
throw new Error(
|
|
'An error happened. The developers have been notified. Please try again later.'
|
|
);
|
|
}
|
|
}
|
|
);
|
|
}
|