1
+ import PROFILE from "../models/profilemodels/profile" ;
2
+ import ADDRESS from "../models/profilemodels/Address" ;
3
+ import BILLINGADRESS from "../models/profilemodels/BillingAdress" ;
4
+ import { Request , Response } from "express" ;
5
+ import { CustomRequest } from "../middlewares/verifyToken" ;
6
+ /* eslint-disable @typescript-eslint/no-explicit-any */
7
+
8
+
9
+ class Profiles {
10
+ static async getprofile ( req : Request , res : Response ) {
11
+ try {
12
+ const profilepage : any = await PROFILE . findOne ( { where :{ userId : req . params . userId } ,
13
+ include : [ { model : BILLINGADRESS , as : 'billingAddress' } , { model : ADDRESS , as : 'Address' } ]
14
+
15
+ } )
16
+ res . status ( 200 ) . json ( {
17
+ statusCode : 200 ,
18
+ message : "succesfully retrieved profile data" ,
19
+ data : profilepage
20
+
21
+ } )
22
+ } catch ( error ) {
23
+ console . log ( )
24
+ }
25
+ }
26
+
27
+ static async edit ( req : CustomRequest , res : Response ) {
28
+ const loggedinuser :any = req . user
29
+ console . log ( loggedinuser )
30
+ const profile :any = await PROFILE . findOne ( { where : { userId : loggedinuser . id } } )
31
+ const profileId = profile . id
32
+
33
+ try {
34
+
35
+ const foundProfile : any = await PROFILE . findOne ( { where :{ id : profileId } } )
36
+ if ( foundProfile ) {
37
+ const bAddress = req . body . billingAddress
38
+ const profileDetails = req . body . profileDetails
39
+ const Address = req . body . address
40
+ if ( profileDetails ) {
41
+ await PROFILE . update ( profileDetails , { where :{ id : profileId } } )
42
+ }
43
+ if ( bAddress ) {
44
+ const BA = await BILLINGADRESS . findOne ( { where : { id : profileId } } )
45
+ if ( BA ) {
46
+ await BILLINGADRESS . update ( bAddress , { where : { profileId} } )
47
+ } else {
48
+ await BILLINGADRESS . create ( { ...bAddress , profileId} )
49
+ }
50
+ }
51
+ if ( Address ) {
52
+ const AD = await ADDRESS . findOne ( { where : { profileId} } )
53
+ if ( AD ) {
54
+ await ADDRESS . update ( Address , { where : { profileId} } )
55
+ } else {
56
+ await ADDRESS . create ( { ...Address , profileId} )
57
+ }
58
+ }
59
+ res . status ( 200 ) . json ( {
60
+ statusCode : 200 ,
61
+ message : `updated profile for ${ foundProfile . firstName } ` ,
62
+
63
+ } )
64
+ }
65
+ } catch ( error : any ) {
66
+ res . status ( 400 ) . json ( {
67
+ StatusCode : 400 ,
68
+ message : error . message
69
+ } )
70
+
71
+ }
72
+
73
+
74
+ }
75
+ static async getall ( req : Request , res : Response ) {
76
+ try {
77
+
78
+ const profiles = await PROFILE . findAll ( { attributes : [ "id" , "email" , "firstName" , "lastName" , "userId" ] } )
79
+ res . status ( 200 ) . json ( {
80
+ statusCode : 200 ,
81
+ message : "sucessfully retreived the profiles" ,
82
+ data : profiles
83
+
84
+ } )
85
+
86
+ } catch ( error : any ) {
87
+ res . status ( 200 ) . json ( {
88
+ statusCode : 400 ,
89
+ message : error . message
90
+ } )
91
+
92
+ }
93
+
94
+ }
95
+ }
96
+ export default Profiles
0 commit comments