Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve the entities and the HTTP status codes #56

Merged
merged 4 commits into from
Aug 13, 2023
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
2 changes: 1 addition & 1 deletion test/integration/db.test.ts → src/configs/db.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dataSource } from '../../src/configs/dbConfig'
import { dataSource } from './dbConfig'

export const initConnection = async (): Promise<void> => {
await dataSource
Expand Down
10 changes: 6 additions & 4 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ export const register = async (req: Request, res: Response): Promise<void> => {
res.status(400).json({ error: 'Email and password are required fields' })
}

const newUser = await registerUser(email, password)
res.status(201).json(newUser)
const { statusCode, message, profile } = await registerUser(email, password)

res.status(statusCode).json({ message, profile })
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand All @@ -31,8 +32,9 @@ export const login = async (req: Request, res: Response): Promise<void> => {
res.status(400).json({ error: 'Email and password are required fields' })
}

const tokenData = await loginUser(email, password)
res.json(tokenData)
const { statusCode, message, token } = await loginUser(email, password)

res.status(statusCode).json({ message, token })
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
16 changes: 7 additions & 9 deletions src/controllers/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Request, Response } from 'express'
import { getProfile, updateProfile } from '../services/profile.service'
import { updateProfile } from '../services/profile.service'
import type Profile from '../entities/profile.entity'

export const getProfileHandler = async (
req: Request,
res: Response
): Promise<void> => {
try {
const user = await getProfile(req)
const { user } = req
if (!user) {
res.status(404).json({ message: 'Profile not found' })
}
Expand All @@ -27,18 +28,15 @@ export const updateProfileHandler = async (
res: Response
): Promise<void> => {
try {
const user = await getProfile(req)
const user = req.user as Profile
if (!user) {
res.status(404).json({ message: 'Profile not found' })
}

const updatedProfile = user && (await updateProfile(user, req.body))
const { statusCode, message, profile } =
user && (await updateProfile(user, req.body))

if (!updatedProfile) {
res.status(404).json({ message: 'Profile not found' })
}

res.status(200).json(updatedProfile)
res.status(statusCode).json({ message, profile })
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
36 changes: 36 additions & 0 deletions src/entities/baseEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
BeforeInsert,
BeforeUpdate,
Column,
PrimaryGeneratedColumn
} from 'typeorm'
import { v4 as uuidv4 } from 'uuid'

class BaseEntity {
@PrimaryGeneratedColumn('uuid')
uuid!: string

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date | undefined

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date | undefined

@BeforeInsert()
@BeforeUpdate()
updateTimestamps(): void {
this.updated_at = new Date()
if (!this.uuid) {
this.created_at = new Date()
}
}

@BeforeInsert()
async generateUuid(): Promise<void> {
if (!this.uuid) {
this.uuid = uuidv4()
}
}
}

export default BaseEntity
38 changes: 4 additions & 34 deletions src/entities/category.entity.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
PrimaryGeneratedColumn
} from 'typeorm'
import { v4 as uuidv4 } from 'uuid'
import { Column, Entity } from 'typeorm'
import BaseEntity from './baseEntity'

@Entity('category')
class Category {
@PrimaryGeneratedColumn('uuid')
uuid!: string

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date | undefined

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date | undefined

class Category extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
category: string

constructor(category: string) {
super()
this.category = category
}

@BeforeInsert()
@BeforeUpdate()
updateTimestamps(): void {
this.updated_at = new Date()
if (!this.uuid) {
this.created_at = new Date()
}
}

@BeforeInsert()
async generateUuid(): Promise<void> {
if (!this.uuid) {
this.uuid = uuidv4()
}
}
}

export default Category
38 changes: 4 additions & 34 deletions src/entities/email.entity.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
PrimaryGeneratedColumn
} from 'typeorm'
import { Column, Entity } from 'typeorm'
import { EmailStatusTypes } from '../enums'
import { v4 as uuidv4 } from 'uuid'
import BaseEntity from './baseEntity'

@Entity('email')
class Email {
@PrimaryGeneratedColumn('uuid')
uuid!: string

class Email extends BaseEntity {
@Column({ type: 'varchar', length: 255 })
recipient: string

Expand All @@ -25,39 +16,18 @@ class Email {
@Column({ type: 'enum', enum: EmailStatusTypes })
state: EmailStatusTypes

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date | undefined

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date | undefined

constructor(
recipient: string,
subject: string,
content: string,
state: EmailStatusTypes
) {
super()
this.recipient = recipient
this.subject = subject
this.content = content
this.state = state
}

@BeforeInsert()
@BeforeUpdate()
updateTimestamps(): void {
this.updated_at = new Date()
if (!this.uuid) {
this.created_at = new Date()
}
}

@BeforeInsert()
async generateUuid(): Promise<void> {
if (!this.uuid) {
this.uuid = uuidv4()
}
}
}

export default Email
41 changes: 4 additions & 37 deletions src/entities/mentee.entity.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
JoinColumn,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn
} from 'typeorm'
import { Column, Entity, JoinColumn, ManyToOne, OneToOne } from 'typeorm'
import { MenteeApplication } from '../types'
import Mentor from './mentor.entity'
import profileEntity from './profile.entity'
import { ApplicationStatus } from '../enums'
import { v4 as uuidv4 } from 'uuid'
import BaseEntity from './baseEntity'

@Entity('mentee')
class Mentee {
@PrimaryGeneratedColumn()
uuid!: string

class Mentee extends BaseEntity {
@Column({
type: 'enum',
enum: ApplicationStatus,
Expand All @@ -42,12 +30,6 @@ class Mentee {
@ManyToOne(() => Mentor, (mentor) => mentor.mentees)
mentor: Mentor

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date | undefined

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date | undefined

constructor(
state: ApplicationStatus,
answers: MenteeApplication,
Expand All @@ -56,29 +38,14 @@ class Mentee {
profile: profileEntity,
mentor: Mentor
) {
super()
this.state = state
this.answers = answers
this.certificate_id = certificate_id
this.journal = journal
this.profile = profile
this.mentor = mentor
}

@BeforeInsert()
@BeforeUpdate()
updateTimestamps(): void {
this.updated_at = new Date()
if (!this.uuid) {
this.created_at = new Date()
}
}

@BeforeInsert()
async generateUuid(): Promise<void> {
if (!this.uuid) {
this.uuid = uuidv4()
}
}
}

export default Mentee
43 changes: 5 additions & 38 deletions src/entities/mentor.entity.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
JoinColumn,
OneToMany,
OneToOne,
PrimaryGeneratedColumn
} from 'typeorm'
import { Column, Entity, JoinColumn, OneToMany, OneToOne } from 'typeorm'
import profileEntity from './profile.entity'
import Mentee from './mentee.entity'
import Category from './category.entity'
import { v4 as uuidv4 } from 'uuid'
import { ApplicationStatus } from '../enums'
import BaseEntity from './baseEntity'

@Entity('mentor')
class Mentor {
@PrimaryGeneratedColumn('uuid')
uuid!: string

class Mentor extends BaseEntity {
@Column({
type: 'enum',
enum: ApplicationStatus,
Expand All @@ -29,7 +17,7 @@ class Mentor {
@OneToMany(() => Category, (category) => category.category)
category: Category

@Column({ type: 'json' })
@Column({ type: 'json', select: false })
application: JSON

@Column({ type: 'boolean' })
Expand All @@ -42,12 +30,6 @@ class Mentor {
@OneToMany(() => Mentee, (mentee) => mentee.mentor)
mentees: Mentee[]

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date | undefined

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date | undefined

constructor(
state: ApplicationStatus,
category: Category,
Expand All @@ -56,29 +38,14 @@ class Mentor {
profile: profileEntity,
mentees: Mentee[]
) {
super()
this.state = state
this.category = category
this.application = application
this.availability = availability
this.profile = profile
this.mentees = mentees
}

@BeforeInsert()
@BeforeUpdate()
updateTimestamps(): void {
this.updated_at = new Date()
if (!this.uuid) {
this.created_at = new Date()
}
}

@BeforeInsert()
async generateUuid(): Promise<void> {
if (!this.uuid) {
this.uuid = uuidv4()
}
}
}

export default Mentor
Loading