Skip to content

Commit

Permalink
fix: add cascade on score and comments attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorplobo committed Dec 3, 2023
1 parent 9b280fd commit 3d28b3e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/comment/entities/comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Comment {
@ManyToOne(() => User, user => user.username)
user: User;

@ManyToOne(() => Post, post => post.id)
@ManyToOne(() => Post, post => post.id, { onDelete: "CASCADE" })
post: Post;

@ManyToOne(() => Comment, comment => comment.id, { nullable: true, onDelete: "CASCADE" })
Expand Down
2 changes: 1 addition & 1 deletion src/score/entities/score.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Score {
@ManyToOne(() => User, user => user.username)
user: User;

@ManyToOne(() => Post, post => post.id)
@ManyToOne(() => Post, post => post.id, { onDelete: "CASCADE" })
post: Post;

@ManyToOne(() => Comment, comment => comment.id, { onDelete: "CASCADE" })
Expand Down
6 changes: 6 additions & 0 deletions src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe("UserService", () => {
user.expertises = [""];

mockRepository.findOneBy = async () => null;
mockRepository.findOne = async () => new User();
mockRepository.save = async () => [];
mockAuth.signUp = async () => ({ hash: "", jwt: "" });

Expand All @@ -57,6 +58,7 @@ describe("UserService", () => {
const user = new CreateUserDto();

mockRepository.findOneBy = async () => new User();
mockRepository.findOne = async () => new User();

expect(service.create(user))
.rejects
Expand All @@ -67,6 +69,7 @@ describe("UserService", () => {
const user = new CreateUserDto();
user.username = "teste";

mockRepository.findOne = async () => new User();
mockRepository.findOneBy = async (dto: object) => {
if ("username" in dto) return new User();
return null;
Expand All @@ -82,6 +85,7 @@ describe("UserService", () => {
user.username = "teste";
user.expertises = [];

mockRepository.findOne = async () => new User();
mockRepository.findOneBy = async () => null;

expect(service.create(user))
Expand All @@ -94,6 +98,7 @@ describe("UserService", () => {
user.username = "teste";
user.expertises = ["", "", "", ""];

mockRepository.findOne = async () => new User();
mockRepository.findOneBy = async () => null;

expect(service.create(user))
Expand All @@ -107,6 +112,7 @@ describe("UserService", () => {

mockAuth.signUp = async () => ({ hash: "", jwt: "" });
mockRepository.findOneBy = async () => null;
mockRepository.findOne = async () => new User();
mockRepository.save = async () => {
throw new Error();
};
Expand Down
60 changes: 32 additions & 28 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";
import { InjectRepository } from "@nestjs/typeorm";
import { User } from "./entities/user.entity";
import { Repository } from "typeorm";
import { LessThanOrEqual, Repository } from "typeorm";
import { AuthService } from "src/auth/auth.service";
import { Expertise } from "src/expertise/entities/expertise.entity";
import { ExpertiseException } from "src/expertise/expertise.service";
Expand Down Expand Up @@ -52,25 +52,23 @@ export class UserService {
const { hash, jwt } = await this.authService.signUp(username, password);

try {
const user = new User();
user.name = name;
user.username = username;
user.email = email;
user.hashPassword = hash;
user.expertises = expertises.map(e => {
const expertise = new Expertise();
expertise.title = e;
return expertise;
});

const initialBadge = new Badge();
initialBadge.id = 1;
initialBadge.title = "Aprendiz";
user.currentBadge = initialBadge;

user.badges = [initialBadge];

await this.userRepository.save(user);
await this.userRepository.save({
name,
username,
email,
hashPassword: hash,
expertises: expertises.map(e => {
const expertise = new Expertise();
expertise.title = e;
return expertise;
}),
currentBadge: {
id: 1,
},
badges: [ { id: 1 } ],
} as User);

const user = await this.findOneByUsername(username);

return {
token: jwt,
Expand Down Expand Up @@ -141,11 +139,19 @@ export class UserService {

async addXp(username: string, quantity: number) {
const user = await this.findOneByUsername(username);
const newXp = Math.max(0, user.xp + quantity);

await this.userRepository.update(
{ username },
{ xp: Math.max(0, user.xp + quantity) },
);
const badges = await this.badgeRepository.find({
where: {
xp: LessThanOrEqual(newXp),
},
});

await this.userRepository.save({
username,
xp: newXp,
badges,
});
}

private async findOne(where: Partial<User>) {
Expand All @@ -154,14 +160,12 @@ export class UserService {
relations: {
expertises: true,
currentBadge: true,
badges: true,
}
});

const badges = await this.badgeRepository.find();

if (user) {
const userBadges = badges.filter(badge => badge.xp <= user.xp);
return { ...user, badges: userBadges };
return user;
} else {
throw new UserException("USER DOESNT EXIST");
}
Expand Down

0 comments on commit 3d28b3e

Please sign in to comment.