Skip to content

Commit

Permalink
fix: fix planner item query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan020313 committed Feb 22, 2024
1 parent 40b3828 commit 24487fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
46 changes: 14 additions & 32 deletions src/modules/planners/planners.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import {
PlannerBodyDto,
Expand Down Expand Up @@ -63,50 +63,32 @@ export class PlannersService {
}

body.taken_items_to_copy.forEach(async (item) => {
const targetItems = await this.PlannerRepository.getTakenPlannerItemById(
const targetItem = await this.PlannerRepository.getTakenPlannerItemById(
user,
item,
);
if (targetItems.length == 0) {
throw new BadRequestException('No such planner item');
} else {
const targetItem = targetItems[0];
await this.PlannerRepository.createTakenPlannerItem(
planner,
targetItem.subject_lecture,
targetItem.is_excluded,
);
}
await this.PlannerRepository.createTakenPlannerItem(
planner,
targetItem.subject_lecture,
targetItem.is_excluded,
);
});

body.future_items_to_copy.forEach(async (item) => {
const targetItems = await this.PlannerRepository.getFuturePlannerItemById(
const targetItem = await this.PlannerRepository.getFuturePlannerItemById(
user,
item,
);
if (targetItems.length == 0) {
throw new BadRequestException('No such planner item');
} else {
const targetItem = targetItems[0];
await this.PlannerRepository.createFuturePlannerItem(
planner,
targetItem,
);
}
await this.PlannerRepository.createFuturePlannerItem(planner, targetItem);
});

body.arbitrary_items_to_copy.forEach(async (item) => {
const targetItems =
const targetItem =
await this.PlannerRepository.getArbitraryPlannerItemById(user, item);
if (targetItems.length == 0) {
throw new BadRequestException('No such planner item');
} else {
const targetItem = targetItems[0];
await this.PlannerRepository.createArbitraryPlannerItem(
planner,
targetItem,
);
}
await this.PlannerRepository.createArbitraryPlannerItem(
planner,
targetItem,
);
});

return toJsonPlanner(planner);
Expand Down
31 changes: 24 additions & 7 deletions src/prisma/repositories/planner.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import {
PlannerBodyDto,
Expand Down Expand Up @@ -95,7 +95,7 @@ export class PlannerRepository {
public async getTakenPlannerItemById(
user: session_userprofile,
id: number,
): Promise<TakenPlannerItem[]> {
): Promise<TakenPlannerItem> {
const planner = await this.prisma.planner_planner.findMany({
include: {
planner_takenplanneritem: {
Expand All @@ -111,7 +111,12 @@ export class PlannerRepository {
},
},
});
return planner.map((p) => p.planner_takenplanneritem).flat();
const candidates = planner.map((p) => p.planner_takenplanneritem).flat();
const result = candidates.find((c) => c.id === id);
if (!result) {
throw new NotFoundException();
}
return result;
}

public async createTakenPlannerItem(
Expand All @@ -138,7 +143,7 @@ export class PlannerRepository {
public async getFuturePlannerItemById(
user: session_userprofile,
id: number,
): Promise<FuturePlannerItem[]> {
): Promise<FuturePlannerItem> {
const planner = await this.prisma.planner_planner.findMany({
include: {
planner_futureplanneritem: {
Expand All @@ -154,7 +159,12 @@ export class PlannerRepository {
},
},
});
return planner.map((p) => p.planner_futureplanneritem).flat();
const candidates = planner.map((p) => p.planner_futureplanneritem).flat();
const result = candidates.find((c) => c.id === id);
if (!result) {
throw new NotFoundException();
}
return result;
}

public async createFuturePlannerItem(
Expand Down Expand Up @@ -183,7 +193,7 @@ export class PlannerRepository {
public async getArbitraryPlannerItemById(
user: session_userprofile,
id: number,
): Promise<ArbitraryPlannerItem[]> {
): Promise<ArbitraryPlannerItem> {
const planner = await this.prisma.planner_planner.findMany({
include: {
planner_arbitraryplanneritem: {
Expand All @@ -199,7 +209,14 @@ export class PlannerRepository {
},
},
});
return planner.map((p) => p.planner_arbitraryplanneritem).flat();
const candidates = planner
.map((p) => p.planner_arbitraryplanneritem)
.flat();
const result = candidates.find((c) => c.id === id);
if (!result) {
throw new NotFoundException();
}
return result;
}

public async createArbitraryPlannerItem(
Expand Down

0 comments on commit 24487fb

Please sign in to comment.