Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions db/errors/postgresErrorConstants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export const publicPostgresErrorClasses = ["22", "23"] as const;
export const PUBLIC_POSTGRES_ERROR_CLASSES = ["22", "23"] as const;

// from https://www.postgresql.org/docs/9.3/protocol-error-fields.html
export const postgresErrorSeverities = ["ERROR", "FATAL", "PANIC"] as const;
export const postgresNoticeSeverities = [
export const POSTGRES_ERROR_SEVERITIES = ["ERROR", "FATAL", "PANIC"] as const;
export const POSTGRES_NOTICE_SEVERITIES = [
"WARNING",
"NOTICE",
"DEBUG",
"LOG",
] as const;

// from https://www.postgresql.org/docs/current/errcodes-appendix.html
export const postgresErrorClassToTitleMap = {
export const POSTGRES_ERROR_CLASS_TO_TITLE_MAP = {
"00": "Successful Completion",
"01": "Warning",
"02": "No Data",
Expand Down Expand Up @@ -55,7 +55,7 @@ export const postgresErrorClassToTitleMap = {
XX: "Internal Error",
} as const;

export const postgresErrorCodeToMessageMap = {
export const POSTGRES_ERROR_CODE_TO_MESSAGE_MAP = {
"00000": "successful_completion",

"01000": "warning",
Expand Down
32 changes: 16 additions & 16 deletions db/errors/postgresErrorConstantsParsers.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { zodEnumFromObjKeys } from "@lib/lib";
import { z } from "zod";
import {
postgresErrorClassToTitleMap,
postgresErrorCodeToMessageMap,
postgresErrorSeverities,
postgresNoticeSeverities,
publicPostgresErrorClasses,
POSTGRES_ERROR_CLASS_TO_TITLE_MAP,
POSTGRES_ERROR_CODE_TO_MESSAGE_MAP,
POSTGRES_ERROR_SEVERITIES,
POSTGRES_NOTICE_SEVERITIES,
PUBLIC_POSTGRES_ERROR_CLASSES,
} from "./postgresErrorConstants";

export const postgresErrorCodeParser = zodEnumFromObjKeys(
postgresErrorCodeToMessageMap,
POSTGRES_ERROR_CODE_TO_MESSAGE_MAP,
);

export const publicPostgresErrorClassParser = zodEnumFromObjKeys(
postgresErrorClassToTitleMap,
).extract(publicPostgresErrorClasses);
POSTGRES_ERROR_CLASS_TO_TITLE_MAP,
).extract(PUBLIC_POSTGRES_ERROR_CLASSES);

type PostgresErrorCode = keyof typeof postgresErrorCodeToMessageMap;
type PostgresErrorCode = keyof typeof POSTGRES_ERROR_CODE_TO_MESSAGE_MAP;
type PostgresErrorMessage =
(typeof postgresErrorCodeToMessageMap)[PostgresErrorCode];
(typeof POSTGRES_ERROR_CODE_TO_MESSAGE_MAP)[PostgresErrorCode];

type PostgresErrorClass = keyof typeof postgresErrorClassToTitleMap;
type PostgresErrorClass = keyof typeof POSTGRES_ERROR_CLASS_TO_TITLE_MAP;
type PostgresErrorTitle =
(typeof postgresErrorClassToTitleMap)[PostgresErrorClass];
(typeof POSTGRES_ERROR_CLASS_TO_TITLE_MAP)[PostgresErrorClass];

export function generatePostgresErrorCodeInfo(code: PostgresErrorCode): {
code: PostgresErrorCode;
Expand All @@ -34,12 +34,12 @@ export function generatePostgresErrorCodeInfo(code: PostgresErrorCode): {
const errorClass = code.substring(0, 2) as PostgresErrorClass;
return {
code,
message: postgresErrorCodeToMessageMap[code],
message: POSTGRES_ERROR_CODE_TO_MESSAGE_MAP[code],
class: errorClass,
title: postgresErrorClassToTitleMap[errorClass],
title: POSTGRES_ERROR_CLASS_TO_TITLE_MAP[errorClass],
};
}

export const postgresSeverityParser = z
.enum(postgresErrorSeverities)
.or(z.enum(postgresNoticeSeverities));
.enum(POSTGRES_ERROR_SEVERITIES)
.or(z.enum(POSTGRES_NOTICE_SEVERITIES));
36 changes: 18 additions & 18 deletions src/db-access/expenses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { database } from "@db/setup/queryPostgres";
import { expensesTable } from "@db/tables/expenses";
import {
type ORMResult,
type OrmResult,
handleDatabaseFullfillment,
handleDatabaseRejection,
ormError,
Expand All @@ -23,7 +23,7 @@ import {

export async function insertExpenses(
expenses: NewExpense[],
): Promise<ORMResult<Expense[]>> {
): Promise<OrmResult<Expense[]>> {
return database
.transaction(async (tx) => {
const insertResult = await tx
Expand All @@ -37,7 +37,7 @@ export async function insertExpenses(

export async function paybackExpenses(
expenseIds: ExpenseKey[],
): Promise<ORMResult<Expense[]>> {
): Promise<OrmResult<Expense[]>> {
return database
.transaction(async (tx) => {
const handledExpenses = await tx
Expand All @@ -50,7 +50,7 @@ export async function paybackExpenses(
),
);
if (handledExpenses.length !== 0) {
throw ormError("Couldn't pay back expense, already handled.");
throw ormError("Already handled");
}

const updateResult = await database
Expand All @@ -59,7 +59,7 @@ export async function paybackExpenses(
.where(inArray(expensesTable.id, expenseIds))
.returning();
if (updateResult.length !== expenseIds.length) {
throw ormError("Couldn't update, some id's didn't exist.");
throw ormError("Couln't find all entries");
}
return updateResult;
})
Expand All @@ -68,7 +68,7 @@ export async function paybackExpenses(

export async function rejectExpense(
expenseIds: ExpenseKey[],
): Promise<ORMResult<Expense[]>> {
): Promise<OrmResult<Expense[]>> {
return database
.transaction(async (tx) => {
const handledExpenses = await tx
Expand All @@ -81,7 +81,7 @@ export async function rejectExpense(
),
);
if (handledExpenses.length !== 0) {
throw ormError("Couldn't reject expense, already handled.");
throw ormError("Already handled");
}

const updateResult = await database
Expand All @@ -90,7 +90,7 @@ export async function rejectExpense(
.where(inArray(expensesTable.id, expenseIds))
.returning();
if (updateResult.length !== expenseIds.length) {
throw ormError("Couldn't update, some id's didn't exist.");
throw ormError("Couln't find all entries");
}
return updateResult;
})
Expand All @@ -99,15 +99,15 @@ export async function rejectExpense(

export async function selectExpensesById(
expenseIds: ExpenseKey[],
): Promise<ORMResult<Expense[]>> {
): Promise<OrmResult<Expense[]>> {
return database
.transaction(async (tx) => {
const selectResult = await tx
.select()
.from(expensesTable)
.where(inArray(expensesTable.id, expenseIds));
if (selectResult.length !== expenseIds.length) {
throw ormError("Couldn't select expenses, id's didn't exist.");
throw ormError("Couln't find all entries");
}
return selectResult;
})
Expand All @@ -116,7 +116,7 @@ export async function selectExpensesById(

export async function selectExpenses(
parameters: QueryParameters,
): Promise<ORMResult<Expense[]>> {
): Promise<OrmResult<Expense[]>> {
return database
.transaction(async (tx) => {
let selectResult: Promise<Expense[]>;
Expand All @@ -142,7 +142,7 @@ export async function selectExpenses(

export async function getSumUnprocessed(
timePeriod: datePeriod,
): Promise<ORMResult<string>> {
): Promise<OrmResult<string>> {
return database
.transaction(async (tx) => {
const unprocessedExpences = await tx
Expand All @@ -160,7 +160,7 @@ export async function getSumUnprocessed(
);

if (unprocessedExpences.length !== 1) {
throw ormError("Invalid money numbers from database.");
throw ormError("Wrong database response format");
}

const sumOfValues = unprocessedExpences[0];
Expand All @@ -177,7 +177,7 @@ export async function getSumUnprocessed(

export async function getSumAccepted(
timePeriod: datePeriod,
): Promise<ORMResult<string>> {
): Promise<OrmResult<string>> {
return database
.transaction(async (tx) => {
const acceptedExpences = await tx
Expand All @@ -196,7 +196,7 @@ export async function getSumAccepted(
);

if (acceptedExpences.length !== 1) {
throw ormError("Invalid money numbers from database.");
throw ormError("Wrong database response format");
}

const sumOfValues = acceptedExpences[0];
Expand All @@ -213,7 +213,7 @@ export async function getSumAccepted(

export async function getSumRejected(
timePeriod: datePeriod,
): Promise<ORMResult<string>> {
): Promise<OrmResult<string>> {
return database
.transaction(async (tx) => {
const rejectedExpences = await tx
Expand All @@ -232,7 +232,7 @@ export async function getSumRejected(
);

if (rejectedExpences.length !== 1) {
throw ormError("Invalid money numbers from database.");
throw ormError("Wrong database response format");
}

const sumOfValues = rejectedExpences[0];
Expand All @@ -249,7 +249,7 @@ export async function getSumRejected(

export async function getAveragePaybackTime(
timePeriod: datePeriod,
): Promise<ORMResult<number>> {
): Promise<OrmResult<number>> {
return database
.transaction(async (tx) => {
const result = await tx
Expand Down
8 changes: 4 additions & 4 deletions src/db-access/sponsors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { database } from "@db/setup/queryPostgres";
import { sponsorsTable } from "@db/tables/sponsors";
import {
type ORMResult,
type OrmResult,
handleDatabaseFullfillment,
handleDatabaseRejection,
ormError,
Expand All @@ -12,7 +12,7 @@ import { inArray } from "drizzle-orm";

export async function insertSponsors(
sponsors: NewSponsor[],
): Promise<ORMResult<Sponsor[]>> {
): Promise<OrmResult<Sponsor[]>> {
return database
.transaction(async (tx) => {
const insertResult = await tx
Expand All @@ -26,15 +26,15 @@ export async function insertSponsors(

export async function selectSponsorsById(
sponsorIds: SponsorKey[],
): Promise<ORMResult<Sponsor[]>> {
): Promise<OrmResult<Sponsor[]>> {
return database
.transaction(async (tx) => {
const selectResult = await tx
.select()
.from(sponsorsTable)
.where(inArray(sponsorsTable.id, sponsorIds));
if (selectResult.length !== sponsorIds.length) {
throw ormError("Couldn't select sponsors, id's didn's exist.");
throw ormError("Couln't find all entries");
}
return selectResult;
})
Expand Down
8 changes: 4 additions & 4 deletions src/db-access/team_applications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { database } from "@db/setup/queryPostgres";
import { teamApplicationsTable } from "@db/tables/teamApplication";
import {
type ORMResult,
type OrmResult,
handleDatabaseFullfillment,
handleDatabaseRejection,
} from "@src/error/ormError";
Expand All @@ -15,7 +15,7 @@ import { asc, inArray } from "drizzle-orm";

export const selectTeamApplications = async (
parameters: QueryParameters,
): Promise<ORMResult<TeamApplication[]>> => {
): Promise<OrmResult<TeamApplication[]>> => {
return database
.transaction(async (tx) => {
return await tx
Expand All @@ -31,7 +31,7 @@ export const selectTeamApplications = async (
export const selectTeamApplicationsByTeamId = async (
teamId: TeamKey[],
parameters: QueryParameters,
): Promise<ORMResult<TeamApplication[]>> => {
): Promise<OrmResult<TeamApplication[]>> => {
return database
.transaction(async (tx) => {
const selectResult = await tx
Expand All @@ -47,7 +47,7 @@ export const selectTeamApplicationsByTeamId = async (

export async function insertTeamApplication(
teamApplication: NewTeamApplication[],
): Promise<ORMResult<TeamApplication[]>> {
): Promise<OrmResult<TeamApplication[]>> {
return database
.transaction(async (tx) => {
const insertResult = await tx
Expand Down
Loading