Skip to content

Commit 18b44ae

Browse files
committed
Fix project ids order and duplication bug
1 parent ef1253a commit 18b44ae

File tree

4 files changed

+71
-13
lines changed

4 files changed

+71
-13
lines changed

src/auth/auth.controller.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class AuthController {
4949
// expire the token from the db because the expiration time of the tokens are rather long
5050
res.clearCookie('auth', {
5151
httpOnly: true,
52-
// sameSite: process.env.NODE_ENV === 'staging' ? 'none' : 'lax',
52+
sameSite: process.env.NODE_ENV === 'staging' ? 'none' : 'lax',
5353
domain: process.env.NODE_ENV === 'development' ? undefined : STAGING_API,
5454
secure: true,
5555
});
@@ -90,7 +90,7 @@ export class AuthController {
9090

9191
res.cookie('auth', nonce, {
9292
httpOnly: true,
93-
// sameSite: process.env.NODE_ENV === 'staging' ? 'none' : 'lax',
93+
sameSite: process.env.NODE_ENV === 'staging' ? 'none' : 'lax',
9494
domain: process.env.NODE_ENV === 'development' ? undefined : STAGING_API,
9595
secure: true,
9696
expires: new Date(Date.now() + this.authService.TokenExpirationDuration),

src/flow/flow.controller.ts

+43-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { VoteProjectsDTO } from './dto/voteProjects.dto';
1818
import { VoteCollectionsDTO } from './dto/voteCollections.dto';
1919
import { AuthedReq } from 'src/utils/types/AuthedReq.type';
2020
import { PairsResult } from './dto/pairsResult';
21+
import { sortProjectId } from 'src/utils';
2122

2223
@Controller({ path: 'flow' })
2324
export class FlowController {
@@ -46,12 +47,8 @@ export class FlowController {
4647
@Req() { userId }: AuthedReq,
4748
@Body() { project1Id, project2Id, pickedId }: VoteProjectsDTO,
4849
) {
49-
return this.flowService.voteForProjects(
50-
userId,
51-
project1Id,
52-
project2Id,
53-
pickedId,
54-
);
50+
const [id1, id2] = sortProjectId(project1Id, project2Id);
51+
return await this.flowService.voteForProjects(userId, id1, id2, pickedId);
5552
}
5653

5754
@UseGuards(AuthGuard)
@@ -60,10 +57,11 @@ export class FlowController {
6057
@Req() { userId }: AuthedReq,
6158
@Body() { collection1Id, collection2Id, pickedId }: VoteCollectionsDTO,
6259
) {
63-
return this.flowService.voteForCollections(
60+
const [id1, id2] = sortProjectId(collection1Id, collection2Id);
61+
return await this.flowService.voteForCollections(
6462
userId,
65-
collection1Id,
66-
collection2Id,
63+
id1,
64+
id2,
6765
pickedId,
6866
);
6967
}
@@ -136,4 +134,40 @@ export class FlowController {
136134
where: { user_id: userId },
137135
});
138136
}
137+
138+
// @Get('/correctInvalid')
139+
// async checkForInvalid() {
140+
// const allInvalidVotes = await this.prismaService.projectVote.findMany({
141+
// select: { id: true, project1_id: true, project2_id: true },
142+
// where: {
143+
// project1_id: { lt: this.prismaService.projectVote.fields.project2_id },
144+
// },
145+
// });
146+
147+
// console.log('number of votes:', allInvalidVotes.length);
148+
149+
// for (let i = 0; i < allInvalidVotes.length; i++) {
150+
// const item = allInvalidVotes[i];
151+
// console.log('this id was invalid:', item.id);
152+
// await this.prismaService.projectVote.update({
153+
// where: { id: item.id },
154+
// data: { project1_id: item.project2_id, project2_id: item.project1_id },
155+
// });
156+
// }
157+
158+
// allVotes.forEach((vote, i, votes) => {
159+
// const index = votes.findIndex(
160+
// (vote2) =>
161+
// (vote.id !== vote2.id &&
162+
// vote.user_id === vote2.user_id &&
163+
// vote.project1_id === vote2.project1_id &&
164+
// vote.project2_id === vote2.project2_id) ||
165+
// (vote.id !== vote2.id &&
166+
// vote.user_id === vote2.user_id &&
167+
// vote.project1_id === vote2.project2_id &&
168+
// vote.project2_id === vote2.project1_id),
169+
// );
170+
// if (index !== -1) console.log(vote, votes[index]);
171+
// });
172+
// }
139173
}

src/flow/flow.service.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Injectable,
4+
InternalServerErrorException,
5+
Logger,
6+
} from '@nestjs/common';
27
import { PrismaService } from 'src/prisma.service';
38
import { getPairwiseCombinations, sortCombinations } from 'src/utils';
49
import {
@@ -510,6 +515,11 @@ export class FlowService {
510515
'Collection 1 and collection 2 ids should be different',
511516
);
512517

518+
if (collection1Id > collection2Id)
519+
throw new InternalServerErrorException(
520+
'Conventionally, collection1Id must be less than collection2Id',
521+
);
522+
513523
if (
514524
pickedId !== null &&
515525
pickedId !== collection1Id &&
@@ -540,6 +550,11 @@ export class FlowService {
540550
'Project 1 and project 2 ids should be different',
541551
);
542552

553+
if (project1Id > project2Id)
554+
throw new InternalServerErrorException(
555+
'Conventionally, project1Id must be less than project2Id',
556+
);
557+
543558
if (pickedId !== null && pickedId !== project1Id && pickedId !== project2Id)
544559
throw new BadRequestException('Picked project invalid id');
545560

src/utils/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ export const sortCombinations = (combinations: number[][], order: number[]) => {
8282
return sorted;
8383
};
8484

85-
export const STAGING_API = 'pairwise.iran.liara.run/';
85+
export const sortProjectId = (
86+
project1Id: number,
87+
project2Id: number,
88+
): [number, number] => {
89+
return project1Id > project2Id
90+
? [project2Id, project1Id]
91+
: [project1Id, project2Id];
92+
};
93+
94+
export const STAGING_API = 'pairwise.iran.liara.run';

0 commit comments

Comments
 (0)