Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feature: goals #1

Merged
merged 4 commits into from
Oct 12, 2024
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: 2 additions & 0 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AuthModule } from './features/auth/auth.module';
import { BackgroundsModule } from './features/backgrounds/backgrounds.module';
import { CalendarsModule } from './features/calendar/calendar.module';
import { FocusModule } from './features/focus/focus.module';
import { GoalsModule } from './features/goals/goals.module';
import { GreetingsModule } from './features/greetings/greetings.module';
import { HabitsModule } from './features/habits/habits.module';
import { MoodModule } from './features/mood/mood.module';
Expand Down Expand Up @@ -37,6 +38,7 @@ const { NODE_ENV } = getEnv();
QuotesModule,
TasksModule,
HabitsModule,
GoalsModule,
CalendarsModule,
WeatherModule,
MoodModule,
Expand Down
130 changes: 130 additions & 0 deletions apps/api/src/features/goals/controllers/goals.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { Request } from 'express';

import { Goal } from '@moaitime/database-core';
import { goalsManager } from '@moaitime/database-services';
import { GoalsListSortFieldEnum, SortDirectionEnum } from '@moaitime/shared-common';

import { DeleteDto } from '../../../dtos/delete.dto';
import { AbstractResponseDto } from '../../../dtos/responses/abstract-response.dto';
import { AuthenticatedGuard } from '../../auth/guards/authenticated.guard';
import { CreateGoalDto } from '../dtos/create-goal.dto';
import { ReorderGoalsDto } from '../dtos/reorder-goals.dto';
import { UpdateGoalDto } from '../dtos/update-goal.dto';

@Controller('/api/v1/goals')
export class GoalsController {
@UseGuards(AuthenticatedGuard)
@Get()
async list(@Req() req: Request): Promise<AbstractResponseDto<Goal[]>> {
const search = req.query.search as string;
const sortField = req.query.sortField as GoalsListSortFieldEnum;
const sortDirection = req.query.sortDirection as SortDirectionEnum;
const includeDeleted = req.query.includeDeleted === 'true';

const data = await goalsManager.list(req.user.id, {
search,
sortField,
sortDirection,
includeDeleted,
});

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Post('reorder')
async reorder(@Body() body: ReorderGoalsDto, @Req() req: Request) {
await goalsManager.reorder(req.user.id, body);

return {
success: true,
};
}

@UseGuards(AuthenticatedGuard)
@Get('deleted')
async listDeleted(@Req() req: Request): Promise<AbstractResponseDto<Goal[]>> {
const data = await goalsManager.listDeleted(req.user.id);

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Get(':goalId')
async view(
@Req() req: Request,
@Param('goalId') goalId: string
): Promise<AbstractResponseDto<Goal>> {
const data = await goalsManager.view(req.user.id, goalId);

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Post()
async create(
@Body() body: CreateGoalDto,
@Req() req: Request
): Promise<AbstractResponseDto<Goal>> {
const data = await goalsManager.create(req.user, body);

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Patch(':goalId')
async update(
@Req() req: Request,
@Param('goalId') goalId: string,
@Body() body: UpdateGoalDto
): Promise<AbstractResponseDto<Goal>> {
const data = await goalsManager.update(req.user.id, goalId, body);

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Delete(':goalId')
async delete(
@Req() req: Request,
@Param('goalId') goalId: string,
@Body() body: DeleteDto
): Promise<AbstractResponseDto<Goal>> {
const data = await goalsManager.delete(req.user.id, goalId, body.isHardDelete);

return {
success: true,
data,
};
}

@UseGuards(AuthenticatedGuard)
@Post(':goalId/undelete')
async undelete(
@Req() req: Request,
@Param('goalId') goalId: string
): Promise<AbstractResponseDto<Goal>> {
const data = await goalsManager.undelete(req.user, goalId);

return {
success: true,
data,
};
}
}
5 changes: 5 additions & 0 deletions apps/api/src/features/goals/dtos/create-goal.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CreateGoalSchema } from '@moaitime/shared-common';

import { createZodDto } from '../../../utils/validation-helpers';

export class CreateGoalDto extends createZodDto(CreateGoalSchema) {}
4 changes: 4 additions & 0 deletions apps/api/src/features/goals/dtos/reorder-goals.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class ReorderGoalsDto {
originalGoalId!: string;
newGoalId!: string;
}
5 changes: 5 additions & 0 deletions apps/api/src/features/goals/dtos/update-goal.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { UpdateGoalSchema } from '@moaitime/shared-common';

import { createZodDto } from '../../../utils/validation-helpers';

export class UpdateGoalDto extends createZodDto(UpdateGoalSchema) {}
15 changes: 15 additions & 0 deletions apps/api/src/features/goals/goals.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';

import { AppMiddleware } from '../../middlewares/app.middleware';
import { GoalsController } from './controllers/goals.controller';

@Module({
imports: [],
controllers: [GoalsController],
providers: [],
})
export class GoalsModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AppMiddleware).forRoutes('*');
}
}
20 changes: 20 additions & 0 deletions packages/database-core/migrations/0013_complex_stellaris.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE IF NOT EXISTS "goals" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" text NOT NULL,
"description" text,
"order" integer DEFAULT 0,
"color" text,
"priority" integer,
"areas" text[],
"deleted_at" timestamp,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now(),
"user_id" uuid NOT NULL
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "goals_user_id_idx" ON "goals" ("user_id");--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "goals" ADD CONSTRAINT "goals_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading
Loading