Skip to content

Commit 782abf5

Browse files
committed
fix: change SurveyResult to return isCurrentAccountAnswer property
1 parent 5681eda commit 782abf5

File tree

8 files changed

+48
-10
lines changed

8 files changed

+48
-10
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { SurveyResultModel } from '@/domain/models/survey-result';
22

33
export interface LoadSurveyResultRepository {
4-
loadBySurveyId(surveyId: string): Promise<SurveyResultModel | null>;
4+
loadBySurveyId(
5+
surveyId: string,
6+
accountId: string
7+
): Promise<SurveyResultModel | null>;
58
}

src/data/protocols/db/survey-result/save-survey-result-repository.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { SurveyResultModel } from '@/domain/models/survey-result';
21
import { SaveSurveyResultParams } from '@/domain/usecases/survey-result/save-survey-result';
32

43
export interface SaveSurveyResultRepository {

src/data/usecases/survey-result/load-survey-result/db-load-survey-result.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ export class DbLoadSurveyResult implements LoadSurveyResult {
1111
private readonly loadSurveyByIdRepository: LoadSurveyByIdRepository
1212
) {}
1313

14-
async load(surveyId: string): Promise<SurveyResultModel | null> {
14+
async load(
15+
surveyId: string,
16+
accountId: string
17+
): Promise<SurveyResultModel | null> {
1518
let surveyResult = await this.loadSurveyResultRepository.loadBySurveyId(
16-
surveyId
19+
surveyId,
20+
accountId
1721
);
1822

1923
if (!surveyResult) {
@@ -28,6 +32,7 @@ export class DbLoadSurveyResult implements LoadSurveyResult {
2832
Object.assign({}, answer, {
2933
count: 0,
3034
percent: 0,
35+
isCurrentAccountAnswer: false,
3136
})
3237
),
3338
};

src/data/usecases/survey-result/save-survey-result/db-save-survey-result.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class DbSaveSurveyResult implements SaveSurveyResult {
1414

1515
async save(data: SaveSurveyResultParams): Promise<SurveyResultModel | null> {
1616
await this.saveSurveyResultRepository.save(data);
17-
return this.loadSurveyResultRepository.loadBySurveyId(data.surveyId);
17+
return this.loadSurveyResultRepository.loadBySurveyId(
18+
data.surveyId,
19+
data.accountId
20+
);
1821
}
1922
}

src/domain/models/survey-result.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type SurveyResultAnswerModel = {
1111
count: number;
1212
percent: number;
1313
image?: string;
14+
isCurrentAccountAnswer: boolean;
1415
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SurveyResultModel } from '@/domain/models/survey-result';
22

33
export interface LoadSurveyResult {
4-
load(surveyId: string): Promise<SurveyResultModel | null>;
4+
load(surveyId: string, accountId: string): Promise<SurveyResultModel | null>;
55
}

src/infra/db/mongodb/survey-result/survey-result-mongo-repository.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export class SurveyResultMongoRepository
3030
);
3131
}
3232

33-
async loadBySurveyId(surveyId: string): Promise<SurveyResultModel | null> {
33+
async loadBySurveyId(
34+
surveyId: string,
35+
accountId: string
36+
): Promise<SurveyResultModel | null> {
3437
const surveyResultCollection = await MongoHelper.getCollection(
3538
'surveyResults'
3639
);
@@ -71,6 +74,15 @@ export class SurveyResultMongoRepository
7174
count: {
7275
$sum: 1,
7376
},
77+
currentAccountAnswer: {
78+
$push: {
79+
$cond: [
80+
{ $eq: ['$data.accountId', MongoHelper.objectId(accountId)] },
81+
'$data.answerId',
82+
null,
83+
],
84+
},
85+
},
7486
})
7587
.builder('$project', {
7688
_id: 0,
@@ -110,6 +122,12 @@ export class SurveyResultMongoRepository
110122
else: 0,
111123
},
112124
},
125+
isCurrentAccountAnswer: {
126+
$eq: [
127+
'$$item.answerId',
128+
{ $arrayElemAt: ['$currentAccountAnswer', 0] },
129+
],
130+
},
113131
},
114132
],
115133
},
@@ -152,6 +170,7 @@ export class SurveyResultMongoRepository
152170
answerId: '$answers.answerId',
153171
answer: '$answers.answer',
154172
image: '$answers.image',
173+
isCurrentAccountAnswer: '$answers.isCurrentAccountAnswer',
155174
},
156175
count: {
157176
$sum: '$answers.count',
@@ -169,8 +188,13 @@ export class SurveyResultMongoRepository
169188
answerId: '$_id.answerId',
170189
answer: '$_id.answer',
171190
image: '$_id.image',
172-
count: '$count',
173-
percent: '$percent',
191+
count: {
192+
$round: ['$count'],
193+
},
194+
percent: {
195+
$round: ['$percent'],
196+
},
197+
isCurrentAccountAnswer: '$_id.isCurrentAccountAnswer',
174198
},
175199
})
176200
.builder('$sort', {

src/presentation/controllers/survey-result/load-survey-result/load-survey-result-controller.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export class LoadSurveyResultController implements Controller {
2323
const survey = await this.loadSurveyById.loadById(surveyId);
2424
if (!survey) return forbidden(new InvalidParamError('surveyId'));
2525

26-
const surveyResult = await this.loadSurveyResult.load(surveyId);
26+
const surveyResult = await this.loadSurveyResult.load(
27+
surveyId,
28+
httpRequest.accountId
29+
);
2730
if (!surveyResult) return forbidden(new InvalidParamError('surveyId'));
2831

2932
return ok(surveyResult);

0 commit comments

Comments
 (0)