Skip to content

Commit

Permalink
fix found bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailDeriabin committed Feb 9, 2025
1 parent bff4017 commit 1de5cc5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/dailyTasks/dailyTasks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Delete, Get, Param, Put } from "@nestjs/common";
import {Controller, Delete, Get, HttpCode, Param, Put} from "@nestjs/common";
import { DailyTasksService } from "./dailyTasks.service";
import { LoggedUser } from "../common/decorator/param/LoggedUser.decorator";
import { User } from "../auth/user";
Expand All @@ -21,34 +21,37 @@ export class DailyTasksController {
@UniformResponse(ModelName.DAILY_TASK)
async getClanTasks(@LoggedUser() user: User) {
const clanId = await this.playerService.getPlayerClanId(user.player_id);
return await this.dailyTasksService.basicService.readMany<DailyTaskDto>({
filter: { clanId },
return this.dailyTasksService.readMany({
filter: { clan_id: clanId },
});
}

@Get(":_id")
@Get("/:_id")
@Serialize(DailyTaskDto)
@UniformResponse(ModelName.DAILY_TASK)
async getTask(@Param() param: _idDto) {
return await this.dailyTasksService.basicService.readOneById(param._id);
return this.dailyTasksService.readOneById(param._id);
}

@Put("/:_id")
@Put("/reserve/:_id")
@UniformResponse()
async reserveTask(@Param() param: _idDto, @LoggedUser() user: User) {
const clanId = await this.playerService.getPlayerClanId(user.player_id);
return await this.dailyTasksService.reserveTask(
return this.dailyTasksService.reserveTask(
user.player_id,
param._id,
clanId
);
}

@HttpCode(204)
@Delete("/:_id")
@Serialize(DailyTaskDto)
@UniformResponse(ModelName.DAILY_TASK)
async deleteTask(@Param() param: _idDto, @LoggedUser() user: User) {
const clanId = await this.playerService.getPlayerClanId(user.player_id);
return await this.dailyTasksService.deleteTask(param._id, clanId, user.player_id);
const [result, errors] = await this.dailyTasksService.deleteTask(param._id, clanId, user.player_id);
if(errors)
return [null, errors];
}
}
33 changes: 29 additions & 4 deletions src/dailyTasks/dailyTasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Task } from "./type/task.type";
import { DailyTaskQueue } from "./dailyTask.queue";
import { taskReservedError } from "./errors/taskReserved.error";
import { TaskGeneratorService } from "./taskGenerator.service";
import {TIServiceCreateManyOptions, TReadByIdOptions} from "../common/service/basicService/IService";

@Injectable()
export class DailyTasksService {
Expand All @@ -24,8 +25,8 @@ export class DailyTasksService {
this.refsInModel = [ModelName.PLAYER];
}
public readonly modelName: ModelName;
public readonly basicService: BasicService;
public readonly refsInModel: ModelName[];
private readonly basicService: BasicService;

/**
* Generates a set of tasks for a new clan.
Expand Down Expand Up @@ -65,7 +66,7 @@ export class DailyTasksService {
*/
async reserveTask(playerId: string, taskId: string, clanId: string) {
const [task, error] = await this.basicService.readOne<DailyTaskDto>({
filter: { _id: taskId, clanId },
filter: { _id: taskId, clan_id: clanId },
});
if (error) throw error;
if (task.player_id) throw taskReservedError;
Expand Down Expand Up @@ -96,7 +97,7 @@ export class DailyTasksService {
*/
async deleteTask(taskId: string, clanId: string, playerId: string) {
const newValues = this.taskGenerator.createTaskRandomValues();
const filter: any = { _id: taskId, clanId };
const filter: any = { _id: taskId, clan_id: clanId };
filter.$or = [{ playerId }, { playerId: { $exists: false } }];
return await this.basicService.updateOne(
{
Expand Down Expand Up @@ -128,7 +129,7 @@ export class DailyTasksService {
if (error) throw error;

task.amountLeft--;

if (task.amountLeft === 0) {
await this.deleteTask(task._id.toString(), task.clan_id, playerId);
this.notifier.taskCompleted(playerId, task);
Expand All @@ -142,4 +143,28 @@ export class DailyTasksService {

return task;
}

/**
* Reads a DailyTask by its _id in DB.
*
* @param _id - The Mongo _id of the DailyTask to read.
* @param options - Options for reading the DailyTask.
* @returns DailyTask with the given _id on succeed or an array of ServiceErrors if any occurred.
*/
async readOneById(_id: string, options?: TReadByIdOptions) {
const optionsToApply = options;
if(options?.includeRefs)
optionsToApply.includeRefs = options.includeRefs.filter((ref) => this.refsInModel.includes(ref));
return this.basicService.readOneById<DailyTaskDto>(_id, optionsToApply);
}

/**
* Reads multiple daily tasks from the database based on the provided options.
*
* @param options - Optional settings for the read operation.
* @returns A promise that resolves to a tuple where the first element is an array of ItemDto objects, and the second element is either null or an array of ServiceError objects if something went wrong.
*/
async readMany(options?: TIServiceCreateManyOptions) {
return this.basicService.readMany<DailyTaskDto>(options);
}
}

0 comments on commit 1de5cc5

Please sign in to comment.