1
- import { ObjectId } from 'mongodb' ;
1
+ import { ObjectId , type Filter } from 'mongodb' ;
2
2
import { getCollection } from '../utils/db'
3
3
import { Status , type Mentorship } from '../interfaces/mentorship' ;
4
- import { DataError } from './errors' ;
5
4
import { upsertEntity } from './utils' ;
6
5
import type { EntityPayload } from './types' ;
6
+ import { ChannelName , type User } from '../common/interfaces/user.interface' ;
7
+ import { buildMailToURL , buildSlackURL } from '../utils/contactUrl' ;
8
+
9
+ export const getMentorships = async ( query : Record < string , string | undefined > ) : Promise < any [ ] > => {
10
+ const mentorshipsCollection = getCollection ( 'mentorships' ) ;
11
+
12
+ const userId = query . userId ;
13
+ if ( ! userId ) {
14
+ throw new Error ( 'User ID is required' ) ;
15
+ }
16
+
17
+ // TODO: - validate that either it's admin or the userId is the same as the one in the token
18
+ const filter : Filter < Document > = {
19
+ $or : [
20
+ { mentor : new ObjectId ( userId ) } ,
21
+ { mentee : new ObjectId ( userId ) } ,
22
+ ] ,
23
+ } ;
24
+
25
+ if ( query . id ) {
26
+ try {
27
+ const objectId = new ObjectId ( query . id ) ; // Convert the ID to ObjectId
28
+ filter . _id = objectId ;
29
+ } catch {
30
+ throw new Error ( 'Invalid mentorship ID' ) ;
31
+ }
32
+ }
33
+
34
+ if ( query . from ) {
35
+ filter . createdAt = { $gte : new Date ( query . from ) } ;
36
+ }
37
+
38
+ // Create an aggregation pipeline that includes the full user objects
39
+ return mentorshipsCollection . aggregate ( [
40
+ { $match : filter } ,
41
+ {
42
+ $lookup : {
43
+ from : 'users' ,
44
+ localField : 'mentor' ,
45
+ foreignField : '_id' ,
46
+ as : 'mentorData'
47
+ }
48
+ } ,
49
+ {
50
+ $lookup : {
51
+ from : 'users' ,
52
+ localField : 'mentee' ,
53
+ foreignField : '_id' ,
54
+ as : 'menteeData'
55
+ }
56
+ } ,
57
+ {
58
+ $addFields : {
59
+ mentor : { $arrayElemAt : [ '$mentorData' , 0 ] } ,
60
+ mentee : { $arrayElemAt : [ '$menteeData' , 0 ] } ,
61
+ // Add isMine field that checks if mentee._id equals userId
62
+ isMine : {
63
+ $eq : [
64
+ { $toString : { $arrayElemAt : [ '$menteeData._id' , 0 ] } } ,
65
+ userId
66
+ ]
67
+ } ,
68
+ }
69
+ } ,
70
+ ] ) . toArray ( ) ;
71
+ } ;
7
72
8
73
export const findMentorship = async ( mentorId : ObjectId , userId : ObjectId ) => {
9
- const mentorship = getCollection ( 'mentorships' )
74
+ const mentorship = getCollection < Mentorship > ( 'mentorships' )
10
75
. find ( {
11
- mentor : mentorId ,
12
- mentee : userId
76
+ mentor : new ObjectId ( mentorId ) ,
77
+ mentee : new ObjectId ( userId ) ,
13
78
} ) ;
14
79
15
- return mentorship [ 0 ] ;
80
+ const foundMentorship = await mentorship . toArray ( ) ;
81
+ return foundMentorship [ 0 ] ;
16
82
}
17
83
18
84
export const getOpenRequestsCount = async ( userId : ObjectId ) => {
@@ -30,4 +96,22 @@ export const getOpenRequestsCount = async (userId: ObjectId) => {
30
96
export const upsertMentorship = async ( mentorship : EntityPayload < Mentorship > ) => {
31
97
const upsertedMentorship = upsertEntity < Mentorship > ( 'mentorships' , mentorship ) ;
32
98
return upsertedMentorship ;
33
- }
99
+ }
100
+
101
+ export const getMentorContactURL = ( mentor : User ) => {
102
+ // slack if exists, otherwise email
103
+ const slackId = mentor . channels ?. find ( channel => channel . type === ChannelName . SLACK ) ?. id ;
104
+ return buildSlackURL ( slackId ) || buildMailToURL ( mentor . email ) ;
105
+ }
106
+
107
+ export const getMenteeOpenRequestsCount = async ( menteeId : ObjectId ) => {
108
+ const openRequests = await getCollection ( 'mentorships' )
109
+ . countDocuments ( {
110
+ mentee : menteeId ,
111
+ status : {
112
+ $in : [ Status . NEW , Status . VIEWED ]
113
+ }
114
+ } ) ;
115
+
116
+ return openRequests ;
117
+ }
0 commit comments