Skip to content

Commit f725edd

Browse files
authored
Merge pull request #60 from GeneralMagicio/add-authorWorldId-and-votersParticipated-to-getUserActivities
add authorWorldId and votersParticipated to getUserActivities
2 parents 19fa728 + 7143bdc commit f725edd

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/user/user.dto.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
1+
import {
2+
IsNotEmpty,
3+
IsNumber,
4+
IsObject,
5+
IsOptional,
6+
IsString,
7+
} from 'class-validator';
8+
import { ActionType } from '@prisma/client';
29

310
export class GetUserDataDto {
411
worldID: string;
@@ -18,13 +25,13 @@ export class GetUserActivitiesDto {
1825

1926
export class UserActionDto {
2027
id: number;
21-
type: 'created' | 'voted';
28+
type: ActionType;
2229
pollId: number;
2330
pollTitle: string;
2431
pollDescription: string;
2532
endDate: Date;
2633
votersParticipated: number;
27-
authorUserId: number;
34+
authorWorldId: string;
2835
createdAt: Date;
2936
}
3037

@@ -44,8 +51,16 @@ export class UserVotesResponseDto {
4451
}
4552

4653
export class SetVoteDto {
54+
@IsNotEmpty()
55+
@IsNumber()
4756
pollId: number;
57+
58+
@IsNotEmpty()
59+
@IsString()
4860
worldID: string;
61+
62+
@IsNotEmpty()
63+
@IsObject()
4964
weightDistribution: Record<string, number>;
5065
}
5166

src/user/user.service.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,35 @@ export class UserService {
9696
title: true,
9797
description: true,
9898
endDate: true,
99-
participantCount: true,
10099
authorUserId: true,
101100
},
102101
},
103102
},
104103
});
105-
const actions: UserActionDto[] = userActions.map((action) => ({
106-
id: action.id,
107-
type: action.type.toLowerCase() as 'created' | 'voted',
108-
pollId: action.poll.pollId,
109-
pollTitle: action.poll.title,
110-
pollDescription: action.poll.description ?? '',
111-
endDate: action.poll.endDate,
112-
isActive: action.poll.endDate >= now,
113-
votersParticipated: action.poll.participantCount,
114-
authorUserId: action.poll.authorUserId,
115-
createdAt: action.createdAt,
116-
}));
104+
const actions: UserActionDto[] = await Promise.all(
105+
// TODO: it's a temporary work around, should add count to Poll and User and authorWorldId to UserAction later
106+
userActions.map(async (action) => {
107+
const participantCount = await this.databaseService.userAction.count({
108+
where: { pollId: action.poll.pollId, type: ActionType.VOTED },
109+
});
110+
const authorWorldId = await this.databaseService.user.findUnique({
111+
where: { id: action.poll.authorUserId },
112+
select: { worldID: true },
113+
});
114+
return {
115+
id: action.id,
116+
type: action.type,
117+
pollId: action.poll.pollId,
118+
pollTitle: action.poll.title,
119+
pollDescription: action.poll.description ?? '',
120+
endDate: action.poll.endDate,
121+
isActive: action.poll.endDate >= now,
122+
votersParticipated: participantCount,
123+
authorWorldId: authorWorldId?.worldID || '',
124+
createdAt: action.createdAt,
125+
};
126+
}),
127+
);
117128
return { userActions: actions };
118129
}
119130

0 commit comments

Comments
 (0)