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

{feat}#79 subscribeded jouneys #28

Merged
merged 7 commits into from
Aug 25, 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
4 changes: 2 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { EmailService } from 'src/users/email.service';
expiresIn: '60m',
},
}),
UsersModule,
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
{ name: 'RefreshToken', schema: RefreshTokenSchema },
{ name: 'ResetToken', schema: ResetTokenSchema },
]),
UsersModule,
],
providers: [
AuthService,
Expand All @@ -37,6 +37,6 @@ import { EmailService } from 'src/users/email.service';
EmailService,
],
controllers: [AuthController],
exports: [JwtStrategy, PassportModule, AuthService],
exports: [AuthService, JwtStrategy, PassportModule, JwtModule],
})
export class AuthModule {}
1 change: 1 addition & 0 deletions src/users/interface/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface User extends Document {
isVerified?: boolean;
role?: UserRole;
journeys?: mongoose.Types.ObjectId[];
subscribedJourneys?: mongoose.Types.ObjectId[];
}
3 changes: 3 additions & 0 deletions src/users/interface/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const UserSchema = new mongoose.Schema(
default: UserRole.ALUNO,
},
journeys: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Journey' }],
subscribedJourneys: [
{ type: mongoose.Schema.Types.ObjectId, ref: 'Journey' },
],
},
{ timestamps: true, collection: 'users' },
);
Expand Down
28 changes: 27 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import {
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dtos/create-user.dto';
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { UserRole } from './dtos/user-role.enum';
import { Roles } from 'src/auth/guards/roles.decorator';
import { RolesGuard } from 'src/auth/guards/roles.guard';
import { UpdateRoleDto } from './dtos/update-role.dto';
import { JwtAuthGuard } from 'src/auth/guards/auth.guard';
import { Types } from 'mongoose';

@Controller('users')
export class UsersController {
Expand Down Expand Up @@ -48,6 +49,13 @@ export class UsersController {
};
}

@Get(':userId/subscribedJourneys')
async getSubscribedJourneys(
@Param('userId') userId: string,
): Promise<Types.ObjectId[]> {
return await this.usersService.getSubscribedJourneys(userId);
}

@Get()
async getUsers() {
return await this.usersService.getUsers();
Expand All @@ -64,6 +72,24 @@ export class UsersController {
}
}

@UseGuards(JwtAuthGuard)
@Post(':userId/subscribe/:journeyId')
async subscribeJourney(
@Param('userId') userId: string,
@Param('journeyId') journeyId: string,
) {
return this.usersService.subscribeJourney(userId, journeyId);
}

@UseGuards(JwtAuthGuard)
@Delete(':userId/unsubscribe/:journeyId')
async unsubscribeJourney(
@Param('userId') userId: string,
@Param('journeyId') journeyId: string,
) {
return this.usersService.unsubscribeJourney(userId, journeyId);
}

@Get('/:id')
async getUserById(@Param('id') id: string) {
try {
Expand Down
12 changes: 10 additions & 2 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { JwtModule } from '@nestjs/jwt';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { MongooseModule } from '@nestjs/mongoose';
import { UserSchema } from './interface/user.schema';
import { EmailService } from './email.service';
import { RefreshTokenSchema } from './interface/refresh-token.schema';
import { ResetTokenSchema } from './interface/reset-token.schema';
import { AuthService } from 'src/auth/auth.service';

@Module({
imports: [
Expand All @@ -14,9 +16,15 @@ import { ResetTokenSchema } from './interface/reset-token.schema';
{ name: 'RefreshToken', schema: RefreshTokenSchema },
{ name: 'ResetToken', schema: ResetTokenSchema },
]),
JwtModule.register({
secret: process.env.JWT_SECRET || 'default_secret',
signOptions: {
expiresIn: '60m',
},
}), // Registrar JwtModule
],
controllers: [UsersController],
providers: [UsersService, EmailService],
providers: [UsersService, EmailService, AuthService],
exports: [UsersService],
})
export class UsersModule {}
50 changes: 50 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,56 @@ export class UsersService {
}
}

async subscribeJourney(userId: string, journeyId: string): Promise<User> {
const user = await this.userModel.findById(userId).exec();
if (!user) {
throw new NotFoundException(`User with ID ${userId} not found`);
}

const objectId = new Types.ObjectId(journeyId);
if (user.subscribedJourneys && user.subscribedJourneys.includes(objectId)) {
throw new ConflictException(
`User already subscribed to journey with ID ${journeyId}`,
);
}

if (!user.subscribedJourneys) {
user.subscribedJourneys = [];
}

if (!user.subscribedJourneys.includes(objectId)) {
user.subscribedJourneys.push(objectId);
}

return user.save();
}

async unsubscribeJourney(userId: string, journeyId: string): Promise<User> {
const user = await this.userModel.findById(userId).exec();
if (!user) {
throw new NotFoundException(`User with ID ${userId} not found`);
}

const objectId = new Types.ObjectId(journeyId);

if (user.subscribedJourneys) {
user.subscribedJourneys = user.subscribedJourneys.filter(
(id) => !id.equals(objectId),
);
}

return user.save();
}

async getSubscribedJourneys(userId: string): Promise<Types.ObjectId[]> {
const user = await this.userModel.findById(userId).exec();
if (!user) {
throw new NotFoundException(`User with ID ${userId} not found`);
}

return user.subscribedJourneys;
}

async findByEmail(email: string): Promise<User | null> {
return this.userModel.findOne({ email }).exec();
}
Expand Down
Loading
Loading