-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add upload and delete avatar API
- Loading branch information
1 parent
e4f01df
commit 3879afc
Showing
46 changed files
with
257 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/backend/src/core/auth/settings/user/services/upload_avatar.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { core_files_avatars } from '@/database/schema/users'; | ||
import { FilesHelperService } from '@/helpers/files/files-helper.service'; | ||
import { InternalDatabaseService } from '@/utils/database/internal_database.service'; | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { eq } from 'drizzle-orm'; | ||
import { UploadAvatarUserSettingsAuthBody } from 'vitnode-shared/auth/settings/user.dto'; | ||
import { User } from 'vitnode-shared/user.dto'; | ||
|
||
@Injectable() | ||
export class UploadAvatarUserSettingsAuthService { | ||
constructor( | ||
private readonly databaseService: InternalDatabaseService, | ||
private readonly filesHelper: FilesHelperService, | ||
) {} | ||
|
||
async uploadAvatar({ | ||
body: { delete_avatar }, | ||
currentUser, | ||
files: { avatar }, | ||
}: { | ||
body: Omit<UploadAvatarUserSettingsAuthBody, 'avatar'>; | ||
currentUser: User; | ||
files: Pick<UploadAvatarUserSettingsAuthBody, 'avatar'>; | ||
}): Promise<void> { | ||
if (!delete_avatar && !avatar) { | ||
throw new BadRequestException('No avatar provided'); | ||
} | ||
|
||
const avatarFromDB = | ||
await this.databaseService.db.query.core_files_avatars.findFirst({ | ||
where: (table, { eq }) => eq(table.user_id, currentUser.id), | ||
}); | ||
|
||
if (avatarFromDB || (delete_avatar && avatarFromDB)) { | ||
await this.filesHelper.delete({ | ||
dir_folder: avatarFromDB.dir_folder, | ||
file_name: avatarFromDB.file_name, | ||
}); | ||
|
||
await this.databaseService.db | ||
.delete(core_files_avatars) | ||
.where(eq(core_files_avatars.user_id, currentUser.id)); | ||
|
||
if (delete_avatar) return; | ||
} | ||
|
||
const file = await this.filesHelper.upload({ | ||
file: avatar, | ||
folder: 'avatars', | ||
plugin_code: 'core', | ||
}); | ||
|
||
await this.databaseService.db.insert(core_files_avatars).values({ | ||
...file, | ||
user_id: currentUser.id, | ||
}); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/backend/src/core/auth/settings/user/user.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Controllers } from '@/helpers/controller.decorator'; | ||
import { FilesValidationPipe } from '@/helpers/files/files.pipe'; | ||
import { UploadFilesMethod } from '@/helpers/upload-files.decorator'; | ||
import { CurrentUser } from '@/helpers/user.decorator'; | ||
import { Body, Put, UploadedFiles } from '@nestjs/common'; | ||
import { ApiOkResponse } from '@nestjs/swagger'; | ||
import { UploadAvatarUserSettingsAuthBody } from 'vitnode-shared/auth/settings/user.dto'; | ||
import { User } from 'vitnode-shared/user.dto'; | ||
|
||
import { UploadAvatarUserSettingsAuthService } from './services/upload_avatar.service'; | ||
|
||
@Controllers({ | ||
plugin_name: 'Core', | ||
plugin_code: 'core', | ||
route: 'auth/settings/user', | ||
isProtect: true, | ||
}) | ||
export class UserSettingsAuthController { | ||
constructor( | ||
private readonly uploadAvatarService: UploadAvatarUserSettingsAuthService, | ||
) {} | ||
|
||
@ApiOkResponse({ | ||
description: 'Upload or delete avatar', | ||
}) | ||
@Put('avatar') | ||
@UploadFilesMethod({ | ||
fields: ['avatar'], | ||
}) | ||
async uploadAvatar( | ||
@UploadedFiles( | ||
new FilesValidationPipe({ | ||
avatar: { | ||
maxSize: 1024 * 1024 * 2, // 2 MB | ||
acceptMimeType: ['image/png', 'image/jpeg', 'image/webp'], | ||
isOptional: true, | ||
maxCount: 1, | ||
}, | ||
}), | ||
) | ||
files: Pick<UploadAvatarUserSettingsAuthBody, 'avatar'>, | ||
@Body() body: UploadAvatarUserSettingsAuthBody, | ||
@CurrentUser() currentUser: User, | ||
): Promise<void> { | ||
await this.uploadAvatarService.uploadAvatar({ | ||
body, | ||
files, | ||
currentUser, | ||
}); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/backend/src/core/auth/settings/user/user.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { UploadAvatarUserSettingsAuthService } from './services/upload_avatar.service'; | ||
import { UserSettingsAuthController } from './user.controller'; | ||
|
||
@Module({ | ||
providers: [UploadAvatarUserSettingsAuthService], | ||
controllers: [UserSettingsAuthController], | ||
}) | ||
export class UserSettingsAuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import * as z from 'zod'; | ||
import { z } from 'zod'; | ||
|
||
export const zodLanguageInput = z.array( | ||
z.object({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ws/admin/views/core/settings/authorization/hooks/use-authorization-settings-form-admin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...c/views/admin/views/core/settings/email/actions/testing/hooks/use-testing-email-admin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.