Skip to content

Commit

Permalink
♻️ refactor: use domain types instead of prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
larwaa committed Mar 15, 2024
1 parent ef573d6 commit 1fc9968
Show file tree
Hide file tree
Showing 95 changed files with 1,087 additions and 777 deletions.
46 changes: 24 additions & 22 deletions src/__tests__/dependencies-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ServerClient } from "postmark";
import { newMockOpenIdClient } from "~/__tests__/mocks/openIdClient.js";
import { BlobStorageAdapter } from "~/adapters/azure-blob-storage.js";
import { env } from "~/config.js";
import type { User } from "~/domain/users.js";
import { User } from "~/domain/users.js";
import prisma from "~/lib/prisma.js";
import type { Services } from "~/lib/server.js";
import { CabinRepository } from "~/repositories/cabins/repository.js";
Expand Down Expand Up @@ -161,27 +161,29 @@ function makeUser(userData: Partial<User> = {}): Promise<User> {
);
const userService = new UserService(userRepository, mailService);

const user = merge<User, Partial<User>>(
{
allergies: faker.word.adjective(),
canUpdateYear: true,
createdAt: new Date(),
email: faker.internet.email({ firstName: faker.string.uuid() }),
feideId: faker.string.uuid(),
firstLogin: false,
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
graduationYear: DateTime.now().plus({ years: 1 }).year,
graduationYearUpdatedAt: null,
id: faker.string.uuid(),
isSuperUser: false,
lastLogin: new Date(),
phoneNumber: faker.phone.number(),
studyProgramId: null,
updatedAt: new Date(),
username: faker.string.uuid(),
},
userData,
const user = new User(
merge(
{
allergies: faker.word.adjective(),
canUpdateYear: true,
createdAt: new Date(),
email: faker.internet.email({ firstName: faker.string.uuid() }),
feideId: faker.string.uuid(),
firstLogin: false,
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
graduationYear: DateTime.now().plus({ years: 1 }).year,
graduationYearUpdatedAt: null,
id: faker.string.uuid(),
isSuperUser: false,
lastLogin: new Date(),
phoneNumber: faker.phone.number(),
studyProgramId: null,
updatedAt: new Date(),
username: faker.string.uuid(),
},
userData,
),
);
const userCreateInput = pick(user, [
"allergies",
Expand Down
113 changes: 106 additions & 7 deletions src/domain/cabins.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { DateTime } from "luxon";

export const BookingStatus = {
const BookingStatus = {
PENDING: "PENDING",
CONFIRMED: "CONFIRMED",
CANCELLED: "CANCELLED",
REJECTED: "REJECTED",
} as const;

export type BookingStatus = (typeof BookingStatus)[keyof typeof BookingStatus];
type BookingStatusType = (typeof BookingStatus)[keyof typeof BookingStatus];

const BookingSemesterEnum = {
SPRING: "SPRING",
FALL: "FALL",
} as const;
type BookingSemesterEnumType =
(typeof BookingSemesterEnum)[keyof typeof BookingSemesterEnum];

interface BookingType {
id: string;
Expand All @@ -18,7 +25,7 @@ interface BookingType {
lastName: string;
phoneNumber: string;
cabins: { id: string }[];
status: BookingStatus;
status: BookingStatusType;
totalCost: number;
internalParticipantsCount: number;
externalParticipantsCount: number;
Expand Down Expand Up @@ -51,7 +58,7 @@ class Booking implements BookingType {
internalParticipantsCount: number;
externalParticipantsCount: number;
totalCost: number;
status: BookingStatus;
status: BookingStatusType;
id: string;
startDate: Date;
endDate: Date;
Expand All @@ -71,7 +78,7 @@ type NewBookingParams = {
lastName: string;
phoneNumber: string;
cabins: { id: string }[];
status: BookingStatus;
status: BookingStatusType;
totalCost: number;
internalParticipantsCount: number;
externalParticipantsCount: number;
Expand Down Expand Up @@ -110,5 +117,97 @@ type CalendarDay = {
availableForCheckOut: boolean;
};

export { Booking };
export type { NewBookingParams, BookingType, CalendarDay, CalendarMonth };
class Cabin {
id: string;
name: string;
capacity: number;
internalPrice: number;
internalPriceWeekend: number;
externalPrice: number;
externalPriceWeekend: number;
createdAt: Date;

constructor(params: {
id: string;
name: string;
capacity: number;
internalPrice: number;
internalPriceWeekend: number;
externalPrice: number;
externalPriceWeekend: number;
createdAt: Date;
}) {
this.id = params.id;
this.name = params.name;
this.capacity = params.capacity;
this.internalPrice = params.internalPrice;
this.internalPriceWeekend = params.internalPriceWeekend;
this.externalPrice = params.externalPrice;
this.externalPriceWeekend = params.externalPriceWeekend;
this.createdAt = params.createdAt;
}
}

class BookingSemester {
semester: BookingSemesterEnumType;
startAt: Date;
endAt: Date;
updatedAt: Date;
id: string;
bookingsEnabled: boolean;

constructor(params: {
semester: BookingSemesterEnumType;
startAt: Date;
endAt: Date;
updatedAt: Date;
id: string;
bookingsEnabled: boolean;
}) {
this.semester = params.semester;
this.startAt = params.startAt;
this.endAt = params.endAt;
this.updatedAt = params.updatedAt;
this.id = params.id;
this.bookingsEnabled = params.bookingsEnabled;
}
}

class BookingContact {
id: string;
name: string;
email: string;
phoneNumber: string;
updatedAt: Date;

constructor(params: {
id: string;
name: string;
email: string;
phoneNumber: string;
updatedAt: Date;
}) {
this.id = params.id;
this.name = params.name;
this.email = params.email;
this.phoneNumber = params.phoneNumber;
this.updatedAt = params.updatedAt;
}
}

export {
Booking,
Cabin,
BookingSemester,
BookingContact,
BookingStatus,
BookingSemesterEnum,
};
export type {
NewBookingParams,
BookingType,
CalendarDay,
CalendarMonth,
BookingStatusType,
BookingSemesterEnumType,
};
13 changes: 10 additions & 3 deletions src/domain/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
export type { EventType, EventUpdateFn } from "./event.js";
export { Event, EventTypeEnum } from "./event.js";
export { Event, EventTypeEnum, AlreadySignedUpError } from "./event.js";

export type { SlotType } from "./slot.js";
export { Slot } from "./slot.js";

export type { SignUpAvailability } from "./sign-ups.js";
export { signUpAvailability } from "./sign-ups.js";
export type {
SignUpAvailability,
EventParticipationStatusType,
} from "./sign-ups.js";
export {
signUpAvailability,
EventParticipationStatus,
EventSignUp,
} from "./sign-ups.js";

export type { CategoryType } from "./category.js";
51 changes: 50 additions & 1 deletion src/domain/events/sign-ups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,53 @@ const signUpAvailability = {
type SignUpAvailability =
(typeof signUpAvailability)[keyof typeof signUpAvailability];

export { type SignUpAvailability, signUpAvailability };
const EventParticipationStatus = {
// CONFIRMED means that the user is signed up for the event.
CONFIRMED: "CONFIRMED",
// ON_WAITLIST means that the user is on the waitlist for the event.
ON_WAITLIST: "ON_WAITLIST",
// REMOVED means that the user has been removed from the event, typically by the event organizer.
REMOVED: "REMOVED",
// RETRACTED means that the user has retracted their sign up.
RETRACTED: "RETRACTED",
} as const;

type EventParticipationStatusType =
(typeof EventParticipationStatus)[keyof typeof EventParticipationStatus];

class EventSignUp {
id: string;
version: number;

active: boolean;
participationStatus: EventParticipationStatusType;
userProvidedInformation: string;

createdAt: Date;
updatedAt: Date;

userId: string;
eventId: string;
slotId: string | null;
orderId: string | null;

constructor(
params: Omit<EventSignUp, "userProvidedInformation"> &
Partial<Pick<EventSignUp, "userProvidedInformation">>,
) {
this.id = params.id;
this.version = params.version;
this.active = params.active;
this.participationStatus = params.participationStatus;
this.userProvidedInformation = params.userProvidedInformation ?? "";
this.createdAt = params.createdAt;
this.updatedAt = params.updatedAt;
this.userId = params.userId;
this.eventId = params.eventId;
this.slotId = params.slotId;
this.orderId = params.orderId;
}
}

Check warning on line 88 in src/domain/events/sign-ups.ts

View check run for this annotation

Codecov / codecov/patch

src/domain/events/sign-ups.ts#L56-L88

Added lines #L56 - L88 were not covered by tests

export type { SignUpAvailability, EventParticipationStatusType };
export { signUpAvailability, EventParticipationStatus, EventSignUp };
22 changes: 22 additions & 0 deletions src/domain/listings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Listing {
id: string;
name: string;
description: string;
closesAt: Date;
applicationUrl: string;
organizationId: string;

constructor(
params: Omit<Listing, "description" | "applicationUrl"> &
Partial<Pick<Listing, "description" | "applicationUrl">>,
) {
this.id = params.id;
this.name = params.name;
this.description = params.description ?? "";
this.closesAt = params.closesAt;
this.applicationUrl = params.applicationUrl ?? "";
this.organizationId = params.organizationId;
}
}

export { Listing };

Check warning on line 22 in src/domain/listings.ts

View check run for this annotation

Codecov / codecov/patch

src/domain/listings.ts#L2-L22

Added lines #L2 - L22 were not covered by tests
52 changes: 50 additions & 2 deletions src/domain/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
export const Role = {
const OrganizationRole = {
ADMIN: "ADMIN",
MEMBER: "MEMBER",
} as const;
type OrganizationRoleType =
(typeof OrganizationRole)[keyof typeof OrganizationRole];

export type Role = (typeof Role)[keyof typeof Role];
const FeaturePermission = {
ARCHIVE_VIEW_DOCUMENTS: "ARCHIVE_VIEW_DOCUMENTS",
ARCHIVE_WRITE_DOCUMENTS: "ARCHIVE_WRITE_DOCUMENTS",
EVENT_WRITE_SIGN_UPS: "EVENT_WRITE_SIGN_UPS",
CABIN_ADMIN: "CABIN_ADMIN",
} as const;

type FeaturePermissionType =
(typeof FeaturePermission)[keyof typeof FeaturePermission];

class Organization {
id: string;
name: string;
description: string;
logoFileId?: string | null;
featurePermissions: FeaturePermissionType[];

constructor(params: Organization) {
this.id = params.id;
this.name = params.name;
this.description = params.description;
this.logoFileId = params.logoFileId;
this.featurePermissions = params.featurePermissions;
}
}

class OrganizationMember {
id: string;
userId: string;
organizationId: string;
role: OrganizationRoleType;

constructor(params: OrganizationMember) {
this.id = params.id;
this.userId = params.userId;
this.organizationId = params.organizationId;
this.role = params.role;
}
}

export {
OrganizationRole,
FeaturePermission,
Organization,
OrganizationMember,
};
export type { OrganizationRoleType, FeaturePermissionType };
Loading

0 comments on commit 1fc9968

Please sign in to comment.