1
1
import { Injectable } from '@nestjs/common' ;
2
2
import axios from 'axios' ;
3
- import { BadRequestException } from '@nestjs/common' ;
3
+ import {
4
+ BadRequestException ,
5
+ InternalServerErrorException ,
6
+ } from '@nestjs/common' ;
4
7
import {
5
8
GITHUB_CLIENT_ID ,
6
9
GITHUB_CLIENT_SECRET ,
@@ -9,10 +12,15 @@ import {
9
12
import { PrismaService } from 'prisma/prisma.service' ;
10
13
import { GithubCallbackQuery } from './constants/profile.types' ;
11
14
import * as jwt from 'jsonwebtoken' ;
15
+ import { decode } from './constants/decode' ;
16
+ import { CloudinaryService } from './cloudinary/cloudinary.service' ;
12
17
13
18
@Injectable ( )
14
19
export class AppService {
15
- constructor ( private prisma : PrismaService ) { }
20
+ constructor (
21
+ private prisma : PrismaService ,
22
+ private cloudinary : CloudinaryService ,
23
+ ) { }
16
24
17
25
getHello ( ) : string {
18
26
return 'Hello from B-704' ;
@@ -88,4 +96,30 @@ export class AppService {
88
96
throw new Error ( 'Github code is either invalid or expired' ) ;
89
97
}
90
98
}
99
+
100
+ async uploadProfilePicture (
101
+ file : Express . Multer . File ,
102
+ token : string ,
103
+ ) : Promise < string > {
104
+ console . log ( file ) ;
105
+ try {
106
+ const user = await decode ( token , this . prisma ) ;
107
+ const upload = await this . cloudinary . uploadImage ( file ) ;
108
+ if ( user . profilePicturePublicId ) {
109
+ await this . cloudinary . deleteImage ( user . profilePicturePublicId ) ;
110
+ }
111
+ await this . prisma . user . update ( {
112
+ where : { id : user . id } ,
113
+ data : {
114
+ profilePicture : upload . secure_url ,
115
+ profilePicturePublicId : upload . public_id ,
116
+ } ,
117
+ } ) ;
118
+ return upload . secure_url ;
119
+ } catch ( error ) {
120
+ throw new InternalServerErrorException (
121
+ error . message || 'Internal error, could not upload profile picture' ,
122
+ ) ;
123
+ }
124
+ }
91
125
}
0 commit comments