Skip to content

Commit

Permalink
Add TypeORM Response table
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1618 committed Dec 24, 2024
1 parent a26e6bc commit 7748600
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
30 changes: 30 additions & 0 deletions server/models/ResponseModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BaseEntity, Column, CreateDateColumn, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
import { UserModel } from "./UserModel";
import { FormType } from "../types/Enums";
import { Uuid } from "../types/Internal";

@Entity('Response')
export class ResponseModel extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
uuid: Uuid;

@ManyToOne((type) => UserModel, (user) => user.response, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'user' })
@Index('response_by_user_index')
user: UserModel;

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

@Column({
type: 'enum',
enum: FormType,
})
formType: FormType;

@Column({type: 'json'})
data: object;
}
11 changes: 8 additions & 3 deletions server/models/UserModel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';
import { Column, Entity, OneToMany, PrimaryColumn } from 'typeorm';
import { ApplicationStatus, UserAccessType } from '../types/Enums';
import { ResponseModel } from './ResponseModel';
import { Uid } from '../types/Internal';

@Entity()
@Entity('User')
export class UserModel {
@PrimaryColumn()
uid: string;
uid: Uid;

@Column()
email: string;
Expand All @@ -29,6 +31,9 @@ export class UserModel {
})
applicationStatus: ApplicationStatus;

@OneToMany((type) => ResponseModel, (response) => response.user, { cascade: true })
response: ResponseModel;

public isRestricted(): boolean {
return this.accessType === UserAccessType.RESTRICTED;
}
Expand Down
3 changes: 2 additions & 1 deletion server/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserModel } from './UserModel';
import { ResponseModel } from './ResponseModel';

export const models = [UserModel];
export const models = [UserModel, ResponseModel];
21 changes: 21 additions & 0 deletions server/repositories/ResponseRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Container from 'typedi';
import { DataSource, EntityRepository, In, SelectQueryBuilder } from 'typeorm';
import { UserModel } from '../models/UserModel';
import { Uuid } from '../types/Internal';
import { ResponseModel } from '../models/ResponseModel';

export const ResponseRepository = Container.get(DataSource)
.getRepository(ResponseModel)
.extend({
async findAll(): Promise<ResponseModel[]> {
return this.find();
},

async findByUuid(uuid: Uuid): Promise<ResponseModel | null> {
return this.findOneBy({ uuid });
},

async findResponsesForUser(user: UserModel): Promise<ResponseModel[]> {
return this.findBy({ user })
}
});
5 changes: 5 additions & 0 deletions server/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Service } from 'typedi';
import { DataSource, EntityManager } from 'typeorm';
import { UserRepository } from './UserRepository';
import { ResponseRepository } from './ResponseRepository';

export class Repositories {
public static user(entityManager: EntityManager) {
return entityManager.withRepository(UserRepository);
}

public static response(entityManager: EntityManager) {
return entityManager.withRepository(ResponseRepository)
}
}

@Service()
Expand Down
21 changes: 21 additions & 0 deletions server/services/ResponseService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Service } from 'typedi';
import { Repositories, TransactionsManager } from '../repositories';
import { Uuid } from '../types/Internal';
import { ResponseModel } from '../models/ResponseModel';

@Service()
export class ResponseService {
private transactionsManager: TransactionsManager;

constructor(transactionsManager: TransactionsManager) {
this.transactionsManager = transactionsManager;
}

public async findByUuid(uuid: Uuid): Promise<ResponseModel> {
const response = await this.transactionsManager.readOnly(
async (entityManager) => Repositories.response(entityManager).findByUuid(uuid),
);
if (!response) throw new Error('Response not found');
return response;
}
}
4 changes: 4 additions & 0 deletions server/types/Enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export enum ApplicationStatus {
REJECTED = 'REJECTED',
ALL_DONE = 'ALL_DONE',
}

export enum FormType {
APPLICATION = 'APPLICATION'
}

0 comments on commit 7748600

Please sign in to comment.