Skip to content

Commit

Permalink
User update is now PATCH method
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 9, 2024
1 parent fb87b78 commit ff9a647
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 125 deletions.
19 changes: 0 additions & 19 deletions src/minecraft/minecraft.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,6 @@ export class MinecraftService {
};
}


async changeValid(session: Session, state: boolean) {
/* switch displaying nick in search */

const minecraft = await this.prisma.minecraft.findFirst({ where: { userId: session.user.id } });

if (!minecraft) return {
statusCode: 404,
message: 'Minecraft account not connected',
message_ru: 'Аккаунт Minecraft не привязан'
};

const result = await this.prisma.minecraft.update({
where: { id: minecraft.id },
data: { valid: state }
})
return { statusCode: 200, new_data: result.valid };
}

async getByCode(code: string): Promise<{ nickname: string, UUID: string } | null> {
const response = await axios.get(`https://mc-oauth.andcool.ru/code/${code}`, { validateStatus: () => true });
if (response.status !== 200) return null;
Expand Down
3 changes: 2 additions & 1 deletion src/root/root.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { CacheModule } from '@nestjs/cache-manager';
ConfigModule.forRoot();

@Module({
providers: [{ provide: APP_GUARD, useClass: ThrottlerGuard },
providers: [
{ provide: APP_GUARD, useClass: ThrottlerGuard },
UserService,
PrismaService,
BandageService,
Expand Down
15 changes: 0 additions & 15 deletions src/user/dto/queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import { Type } from 'class-transformer';
import { IsNotEmpty, IsNumber, IsOptional, Max, Min } from 'class-validator';
import { IsBooleanStr } from 'src/common/types.decorator';

export class StateQueryDTO {
@IsBooleanStr()
@IsNotEmpty()
state?: string;
}

export class SetQueryDTO {
@IsBooleanStr()
@IsNotEmpty()
Expand All @@ -29,12 +23,3 @@ export class PageTakeQueryDTO {
page?: number;
}


export class ThemeQueryDTO {
@IsNumber()
@Type(() => Number)
@IsNotEmpty()
@Min(0)
@Max(2)
theme?: number;
}
22 changes: 21 additions & 1 deletion src/user/dto/updateUser.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { IsBoolean, IsOptional } from 'class-validator';
import { IsBoolean, IsNumber, IsOptional, Max, Min } from 'class-validator';

export class UpdateUsersDto {
@IsOptional()
@IsBoolean()
banned?: boolean;
}

export class UpdateSelfUserDto {
@IsNumber()
@IsOptional()
@Min(0)
@Max(2)
theme?: number;

@IsBoolean()
@IsOptional()
skin_autoload?: boolean

@IsBoolean()
@IsOptional()
nick_search?: boolean

@IsBoolean()
@IsOptional()
public?: boolean
}
86 changes: 23 additions & 63 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
UseGuards,
ValidationPipe,
UsePipes,
StreamableFile
StreamableFile,
Patch
} from '@nestjs/common';
import type { Response } from 'express'
import { AuthGuard } from 'src/guards/auth.guard';
Expand All @@ -24,9 +25,9 @@ import { RolesGuard } from 'src/guards/roles.guard';
import { AuthEnum, RolesEnum } from 'src/interfaces/types';
import { Auth } from 'src/decorators/auth.decorator';
import { Roles } from 'src/decorators/access.decorator';
import { UpdateUsersDto } from './dto/updateUser.dto';
import { UpdateSelfUserDto, UpdateUsersDto } from './dto/updateUser.dto';
import { RequestSession } from 'src/common/bandage_response';
import { PageTakeQueryDTO, StateQueryDTO, ThemeQueryDTO } from './dto/queries.dto';
import { PageTakeQueryDTO } from './dto/queries.dto';

@Controller()
@UseGuards(AuthGuard, RolesGuard)
Expand Down Expand Up @@ -98,51 +99,6 @@ export class UserController {
res.send(data);
}

@Put("/user/me/theme")
@Auth(AuthEnum.Strict)
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async profile_theme(
@Req() request: RequestSession,
@Res() res: Response,
@Body() body: ThemeQueryDTO
): Promise<void> {
/* update profile theme */

await this.userService.setProfileTheme(request.session, body.theme as number);
res.status(200).send({
statusCode: 200,
new_theme: body.theme
})
}

@Put("/user/me/connections/minecraft/valid")
@Auth(AuthEnum.Strict)
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async set_valid(
@Req() request: RequestSession,
@Res() res: Response,
@Query() query: StateQueryDTO
): Promise<void> {
/* set displaying nickname in search */

const data = await this.minecraftService.changeValid(request.session, query.state === 'true');
res.status(data.statusCode).send(data);
}

@Put("/user/me/connections/minecraft/autoload")
@Auth(AuthEnum.Strict)
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async set_autoload(
@Req() request: RequestSession,
@Res() res: Response,
@Query() query: StateQueryDTO
): Promise<void> {
/* set skin autoload in editor */

const data = await this.userService.changeAutoload(request.session, query.state === 'true');
res.status(data.statusCode).send(data);
}

@Post("/user/me/connections/minecraft/connect/:code")
@Auth(AuthEnum.Strict)
async connectMinecraft(
Expand Down Expand Up @@ -243,20 +199,6 @@ export class UserController {
res.status(data.statusCode).send(data);
}

@Put("/user/me/settings/public")
@Auth(AuthEnum.Strict)
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async set_public(
@Req() request: RequestSession,
@Res() res: Response,
@Query() query: StateQueryDTO
): Promise<void> {
/* set skin autoload in editor */

const data = await this.userService.setPublic(request.session, query.state === 'true');
res.status(data.statusCode).send(data);
}

@Get('/users')
@Auth(AuthEnum.Strict)
@Roles([RolesEnum.UpdateUsers])
Expand All @@ -268,7 +210,11 @@ export class UserController {
@Auth(AuthEnum.Strict)
@Roles([RolesEnum.UpdateUsers])
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async update_user(@Param('username') username: string, @Res() res: Response, @Body() body: UpdateUsersDto) {
async update_user(
@Param('username') username: string,
@Res() res: Response,
@Body() body: UpdateUsersDto
) {
const data = await this.userService.updateUser(username, body);
res.status(data.statusCode).send(data);
}
Expand All @@ -293,4 +239,18 @@ export class UserController {
res.setHeader('Content-Type', 'image/png');
return new StreamableFile(Buffer.from(cache, "base64"));
}

@Patch('/user/me')
@Auth(AuthEnum.Strict)
@UsePipes(new ValidationPipe({ whitelist: true, transform: true }))
async update_me(
@Req() request: RequestSession,
@Res() res: Response,
@Body() body: UpdateSelfUserDto
) {
/* Update self data */

const data = await this.userService.updateSelfUser(request.session, body);
res.status(data.statusCode).send(data);
}
}
68 changes: 42 additions & 26 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Inject, Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import axios from 'axios';
import { hasAccess, Session } from 'src/auth/auth.service';
import { UpdateUsersDto } from './dto/updateUser.dto';
import { UpdateSelfUserDto, UpdateUsersDto } from './dto/updateUser.dto';
import { generateResponse } from 'src/common/bandage_response';
import { RolesEnum } from 'src/interfaces/types';
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { days } from '@nestjs/throttler';

const discord_url = process.env.DISCORD_URL + "/api/v10";

Expand Down Expand Up @@ -347,31 +348,6 @@ export class UserService {
}
}

async setProfileTheme(session: Session, theme: number) {
await this.prisma.userSettings.update({ where: { userId: session.user.id }, data: { profile_theme: theme } });
}

async changeAutoload(session: Session, state: boolean) {
/* switch skin autoload in editor */

const result = await this.prisma.userSettings.update({
where: { userId: session.user.id }, data: { autoload: state }
})
return {
statusCode: 200,
new_data: result.autoload
};
}

async setPublic(session: Session, state: boolean) {
/* change profile visibility */

const result = await this.prisma.userSettings.update({
where: { userId: session.user.id }, data: { public_profile: state }
})
return { statusCode: 200, new_data: result.public_profile };
}

async getUsers() {
const users = await this.prisma.user.findMany({ include: { UserSettings: true, AccessRoles: true } });

Expand Down Expand Up @@ -407,5 +383,45 @@ export class UserService {
message_ru: 'Обновлён'
}
}

async updateSelfUser(session: Session, body: UpdateSelfUserDto) {
/* Update self data */
/* TODO: Nickname changing */

if (
body.theme !== undefined ||
body.skin_autoload !== undefined ||
body.public !== undefined
) {
await this.prisma.userSettings.update({
where: { userId: session.user.id },
data: {
profile_theme: body.theme,
autoload: body.skin_autoload,
public_profile: body.public
}
});
}

if (body.nick_search !== undefined) {
if (!session.user.profile) {
return {
statusCode: 400,
message: 'Minecraft profile didn\'t connected',
message_ru: 'К вашему профилю не подключена учетная запись Minecraft'
}
}
await this.prisma.minecraft.update({
where: { id: session.user.profile.id },
data: { valid: body.nick_search }
});
}

return {
statusCode: 200,
message: 'Patched',
message_ru: 'Обновлен'
}
}
}

0 comments on commit ff9a647

Please sign in to comment.