-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a26e6bc
commit 7748600
Showing
7 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters