-
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.
[NestJS] Milestone 6: Plot twist! Special treat to Wakandians.
- Loading branch information
1 parent
bc563b0
commit fd7995e
Showing
10 changed files
with
174 additions
and
21 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
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
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
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { ProfileModule } from '../profile/profile.module'; | ||
import { PromoCode } from './promo-code.entity'; | ||
import { PromoCodeService } from './promo-code.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([PromoCode]), ProfileModule], | ||
providers: [PromoCodeService], | ||
exports: [PromoCodeService], | ||
}) | ||
export class PromoCodeModule {} |
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,16 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; | ||
import { Profile } from '../profile/profile.entity'; | ||
|
||
@Entity() | ||
export class PromoCode { | ||
@PrimaryGeneratedColumn() | ||
id: string; | ||
|
||
@Column() | ||
code: string; | ||
|
||
@ManyToOne(() => Profile, (profile) => profile.relatives, { | ||
onDelete: 'CASCADE', | ||
}) | ||
profile: Profile; | ||
} |
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,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ProfileService } from '../profile/profile.service'; | ||
import { PromoCode } from './promo-code.entity'; | ||
import { Profile } from 'src/profile/profile.entity'; | ||
import * as crypto from 'crypto'; | ||
|
||
@Injectable() | ||
export class PromoCodeService { | ||
constructor( | ||
private readonly profileService: ProfileService, | ||
@InjectRepository(PromoCode) | ||
private relativeRepository: Repository<PromoCode>, | ||
) {} | ||
|
||
async create(profile: Profile): Promise<PromoCode> { | ||
const code = crypto.randomBytes(20).toString('hex'); | ||
const promoCode = this.relativeRepository.create({ code }); | ||
promoCode.profile = profile; | ||
|
||
return this.relativeRepository.save(promoCode); | ||
} | ||
} |
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