1+ import { Types } from "mongoose" ;
2+
13import { GigRequestModel } from "../models/gig-request.model.js" ;
24
35export class GigRequestRepository {
@@ -14,10 +16,46 @@ export class GigRequestRepository {
1416
1517 // Find by ID
1618 async findById ( id : string ) {
17- return GigRequestModel . findById ( id )
18- . populate ( "gigId" )
19- . populate ( "brandId" , "fullName profileImageUrl email" )
20- . populate ( "influencerId" , "fullName profileImageUrl email" ) ;
19+ const results = await GigRequestModel . aggregate ( [
20+ { $match : { _id : new Types . ObjectId ( id ) } } ,
21+ {
22+ $lookup : {
23+ from : "brandprofiles" ,
24+ localField : "brandId" ,
25+ foreignField : "userId" ,
26+ as : "brandProfile" ,
27+ } ,
28+ } ,
29+ {
30+ $lookup : {
31+ from : "influencerprofiles" ,
32+ localField : "influencerId" ,
33+ foreignField : "userId" ,
34+ as : "influencerProfile" ,
35+ } ,
36+ } ,
37+ {
38+ $lookup : {
39+ from : "gigs" ,
40+ localField : "gigId" ,
41+ foreignField : "_id" ,
42+ as : "gigData" ,
43+ } ,
44+ } ,
45+ {
46+ $project : {
47+ _id : 1 ,
48+ brandId : 1 ,
49+ influencerId : 1 ,
50+ gigId : { $arrayElemAt : [ "$gigData" , 0 ] } ,
51+ status : 1 ,
52+ note : 1 ,
53+ brandProfile : { $arrayElemAt : [ "$brandProfile" , 0 ] } ,
54+ influencerProfile : { $arrayElemAt : [ "$influencerProfile" , 0 ] } ,
55+ }
56+ }
57+ ] ) ;
58+ return results [ 0 ] || null ;
2159 }
2260
2361 // Check existing request (prevent duplicate per gig)
@@ -36,15 +74,75 @@ export class GigRequestRepository {
3674
3775 // Get all requests for a user (brand or influencer)
3876 async findMyRequests ( userId : string ) {
39- return GigRequestModel . find ( {
40- $or : [
41- { brandId : userId } ,
42- { influencerId : userId } ,
43- ] ,
44- } )
45- . populate ( "gigId" )
46- . populate ( "brandId" , "fullName profileImageUrl" )
47- . populate ( "influencerId" , "fullName profileImageUrl" )
48- . sort ( { createdAt : - 1 } ) ;
77+ return GigRequestModel . aggregate ( [
78+ {
79+ $match : {
80+ $or : [
81+ { brandId : new Types . ObjectId ( userId ) } ,
82+ { influencerId : new Types . ObjectId ( userId ) } ,
83+ ] ,
84+ } ,
85+ } ,
86+ {
87+ $lookup : {
88+ from : "users" ,
89+ localField : "brandId" ,
90+ foreignField : "_id" ,
91+ as : "brandUser" ,
92+ } ,
93+ } ,
94+ {
95+ $lookup : {
96+ from : "brandprofiles" ,
97+ localField : "brandId" ,
98+ foreignField : "userId" ,
99+ as : "brandProfile" ,
100+ } ,
101+ } ,
102+ {
103+ $lookup : {
104+ from : "users" ,
105+ localField : "influencerId" ,
106+ foreignField : "_id" ,
107+ as : "influencerUser" ,
108+ } ,
109+ } ,
110+ {
111+ $lookup : {
112+ from : "influencerprofiles" ,
113+ localField : "influencerId" ,
114+ foreignField : "userId" ,
115+ as : "influencerProfile" ,
116+ } ,
117+ } ,
118+ {
119+ $lookup : {
120+ from : "gigs" ,
121+ localField : "gigId" ,
122+ foreignField : "_id" ,
123+ as : "gigData" ,
124+ } ,
125+ } ,
126+ {
127+ $project : {
128+ _id : 1 ,
129+ status : 1 ,
130+ note : 1 ,
131+ createdAt : 1 ,
132+ brandId : {
133+ _id : { $arrayElemAt : [ "$brandUser._id" , 0 ] } ,
134+ fullName : { $arrayElemAt : [ "$brandProfile.companyName" , 0 ] } ,
135+ profileImageUrl : { $arrayElemAt : [ "$brandProfile.profileImageUrl" , 0 ] } ,
136+ } ,
137+ influencerId : {
138+ _id : { $arrayElemAt : [ "$influencerUser._id" , 0 ] } ,
139+ fullName : { $arrayElemAt : [ "$influencerProfile.fullName" , 0 ] } ,
140+ profileImageUrl : { $arrayElemAt : [ "$influencerProfile.profileImageUrl" , 0 ] } ,
141+ } ,
142+ gigId : { $arrayElemAt : [ "$gigData" , 0 ] } ,
143+ } ,
144+ } ,
145+ { $sort : { createdAt : - 1 } } ,
146+ ] ) ;
49147 }
50148}
0 commit comments