Skip to content

Commit 75755d9

Browse files
committed
admin: get all mentorships
1 parent f691d8c commit 75755d9

File tree

9 files changed

+53
-39
lines changed

9 files changed

+53
-39
lines changed

netlify/functions-src/functions/common/interfaces/user.interface.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import type { ObjectId } from 'mongodb'
22

33
export interface User {
44
readonly _id: ObjectId;
5-
available?: boolean
6-
auth0Id: string
7-
email: string
8-
name: string
9-
avatar?: string
10-
roles: Role[]
11-
country?: string
5+
available?: boolean;
6+
auth0Id: string;
7+
email: string;
8+
name: string;
9+
avatar?: string;
10+
roles: Role[];
11+
country?: string;
1212
image?: {
13-
filename: string
14-
}
15-
channels?: any[]
13+
filename: string;
14+
};
15+
channels?: any[];
16+
createdAt: Date;
1617
}
1718

1819
export enum Role {

netlify/functions-src/functions/data/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { OptionalId, WithId } from 'mongodb';
22

3+
export type BaseExistingEntity = WithId<{
4+
createdAt: Date;
5+
}>
6+
37
type UpdateEntityPayload<T> = WithId<Partial<T>>;
48
export type CreateEntityPayload<T> = OptionalId<T>;
59
export type EntityPayload<T> = CreateEntityPayload<T> | UpdateEntityPayload<T>;

netlify/functions-src/functions/data/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ObjectId, type Filter, type MatchKeysAndValues, type OptionalId, type OptionalUnlessRequiredId, type WithId } from 'mongodb';
22
import { getCollection } from '../utils/db';
3-
import type { CollectionName, EntityPayload, UpsertResult } from './types';
3+
import type { BaseExistingEntity, CollectionName, EntityPayload, UpsertResult } from './types';
44
import { DataError } from './errors';
55

66
export const upsertEntityByCondition = async <T extends OptionalId<unknown>>(
@@ -24,22 +24,28 @@ export const upsertEntityByCondition = async <T extends OptionalId<unknown>>(
2424
};
2525
}
2626

27-
export async function upsertEntity<T extends WithId<unknown>>(collectionName: CollectionName, entity: EntityPayload<T>): Promise<WithId<T>> {
27+
export async function upsertEntity<T extends BaseExistingEntity>(collectionName: CollectionName, entity: EntityPayload<T>): Promise<WithId<T>> {
2828
const collection = getCollection<T>(collectionName);
2929
const { _id: entityId, ...entityData } = entity;
3030

3131
if (entityId) {
3232
const updatedEntity = await collection.findOneAndUpdate(
3333
{ _id: new ObjectId(entityId) as Filter<T> },
34-
{ $set: entityData as Partial<T> },
34+
{
35+
$set: entityData as Partial<T>,
36+
},
3537
{ returnDocument: "after" }
3638
);
3739
if (!updatedEntity) {
3840
throw new DataError(404, `${collectionName}: entitiy id: '${entity._id}' not found`);
3941
}
4042
return updatedEntity;
4143
} else {
42-
const result = await collection.insertOne(entity as OptionalUnlessRequiredId<T>);
43-
return { ...entity, _id: result.insertedId } as WithId<T>;
44+
const entityPayload = {
45+
...entity as OptionalUnlessRequiredId<T>,
46+
createdAt: new Date(),
47+
};
48+
const result = await collection.insertOne(entityPayload);
49+
return { ...entityPayload, _id: result.insertedId } as WithId<T>;
4450
}
4551
}

netlify/functions-src/functions/mentorships.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { Handler } from '@netlify/functions';
21
import { withDB } from './hof/withDB';
32
import { withAuth } from './utils/auth';
43
import { withRouter } from './hof/withRouter';
54
import { handler as mentorshipsRequestsHandler, updateRequestHandler } from './modules/mentorships/requests'
65
import { handler as getAllMentorshipsHandler } from './modules/mentorships/get-all'
76
import { handler as applyForMentorshipHandler } from './modules/mentorships/apply';
87
import { Role } from './common/interfaces/user.interface';
8+
import type { ApiHandler } from './types';
99

10-
export const handler: Handler = withDB(
10+
export const handler: ApiHandler = withDB(
1111
withRouter([
1212
['/', 'GET', withAuth(getAllMentorshipsHandler, { role: Role.ADMIN })],
1313
['/:userId/requests', 'GET', withAuth(mentorshipsRequestsHandler)],

netlify/functions-src/functions/modules/mentorships/get-all.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,27 @@ const getMentorships = async (query: any): Promise<any[]> => {
1818
filter.createdAt = { $lte: new Date(query.to) };
1919
}
2020

21-
return mentorshipsCollection.find(filter).toArray();
21+
return mentorshipsCollection.aggregate([
22+
{ $match: filter },
23+
{
24+
$lookup: {
25+
from: 'users',
26+
localField: 'mentor',
27+
foreignField: '_id',
28+
as: 'mentor'
29+
}
30+
},
31+
{
32+
$lookup: {
33+
from: 'users',
34+
localField: 'mentee',
35+
foreignField: '_id',
36+
as: 'mentee'
37+
}
38+
},
39+
{ $unwind: { path: '$mentor' } },
40+
{ $unwind: { path: '$mentee' } }
41+
]).toArray();
2242
};
2343

2444
const getAllMentorshipsHandler = async (event: HandlerEvent) => {

netlify/functions-src/functions/modules/mentorships/requests.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,8 @@ const getMentorships = async (query: Record<string, string | undefined>): Promis
6868
userId
6969
]
7070
},
71-
// Add createdAt field derived from _id.getTimestamp()
72-
createdAt: { $toDate: "$_id" }
7371
}
7472
},
75-
{
76-
$project: {
77-
// Include all original fields from the mentorship
78-
_id: 1,
79-
mentor: 1,
80-
mentee: 1,
81-
status: 1,
82-
createdAt: 1, // Include the derived createdAt field
83-
updatedAt: 1,
84-
message: 1,
85-
background: 1,
86-
expectation: 1,
87-
isMine: 1
88-
}
89-
}
9073
]).toArray();
9174
};
9275

@@ -104,7 +87,7 @@ const updateMentorshipHandler = async (event: HandlerEventWithBody<Partial<Mento
10487
reason,
10588
};
10689
const upsertedMentorship = await upsertMentorship(mentorshipToUpsert);
107-
return success({ mentorship: upsertedMentorship });
90+
return success({ data: upsertedMentorship });
10891
} catch (e) {
10992
if (e instanceof DataError) {
11093
return error(e.message, e.statusCode);

netlify/functions-src/mongo-scripts/update-mentorship.mongodb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use('codingcoach');
1111
// { _id: new ObjectId('67e049568a3938d0aac4a216') },
1212
// );
1313
db.getCollection('mentorships').updateOne(
14-
{ _id: new ObjectId('67e140e3bcacb881b788c9eb') },
14+
{ _id: new ObjectId('67e9a7023ce1a19ad81bd5b7') },
1515
{
1616
$set: {
1717
status: 'New',

src/Me/MentorshipRequests/MentorshipRequests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const MentorshipReq = () => {
9090
reason,
9191
listType = 'mentor'
9292
) => {
93-
const { success, mentorship } = await api.updateMentorshipReqStatus(
93+
const { success, data: mentorship } = await api.updateMentorshipReqStatus(
9494
_id,
9595
userId,
9696
{

src/api/admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MentorshipRequest, UserRecord } from '../types/models';
44
export function getAllMentorshipRequests(apiService: ApiService, numMonthAgo: number = 1) {
55
const monthAgo = new Date();
66
monthAgo.setMonth(monthAgo.getMonth() - numMonthAgo);
7-
return apiService.makeApiCall<MentorshipRequest[]>(`${paths.MENTORSHIP}/requests`, {
7+
return apiService.makeApiCall<MentorshipRequest[]>(`${paths.MENTORSHIP}`, {
88
from: monthAgo,
99
});
1010
}

0 commit comments

Comments
 (0)