Skip to content

Commit 5d42ffa

Browse files
committed
fix(db-query): minor fix in dataset action update
MIGRATION CHANGE: migration-20250923054319- added index for unique user action
1 parent 2370288 commit 5d42ffa

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

migrations/pg/migrations/sqls/20250923054319-init-up.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ update
9898
create trigger mdt_chats before
9999
update
100100
on
101-
chatbot.chats for each row execute function chatbot.moddatetime('modified_on');
101+
chatbot.chats for each row execute function chatbot.moddatetime('modified_on');
102+
103+
CREATE UNIQUE INDEX unique_user_action ON chatbot.dataset_actions(user_id, dataset_id);

src/__tests__/db-query/acceptance/dataset.controller.acceptance.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ describe('DatasetController', () => {
160160
.send(updatedData)
161161
.expect(204);
162162

163-
const dataset = await repo.findById(dummyDataset.id, {
164-
include: ['actions'],
165-
});
163+
const dataset = await repo.findById(
164+
dummyDataset.id,
165+
{
166+
include: ['actions'],
167+
},
168+
{skipUserFilter: true},
169+
);
166170
expect(dataset).to.have.property('votes', 1);
167171
expect(dataset.actions).to.be.Array();
168172
expect(dataset.actions).to.have.length(1);
@@ -270,7 +274,7 @@ describe('DatasetController', () => {
270274
.send(updatedData)
271275
.expect(400);
272276
});
273-
it('should should increment votes for different user', async () => {
277+
it('should increment votes for different user', async () => {
274278
await client
275279
.patch(`/datasets/${dummyDataset.id}`)
276280
.set(
@@ -288,14 +292,18 @@ describe('DatasetController', () => {
288292
)
289293
.send({liked: true})
290294
.expect(204);
291-
const dataset = await repo.findById(dummyDataset.id, {
292-
include: ['actions'],
293-
});
295+
const dataset = await repo.findById(
296+
dummyDataset.id,
297+
{
298+
include: ['actions'],
299+
},
300+
{skipUserFilter: true},
301+
);
294302
expect(dataset).to.have.property('votes', 2);
295303
expect(dataset.actions).to.be.Array();
296304
expect(dataset.actions).to.have.length(2);
297305
});
298-
it('should should cancel votes for different user', async () => {
306+
it('should cancel votes for different users', async () => {
299307
await client
300308
.patch(`/datasets/${dummyDataset.id}`)
301309
.set(
@@ -313,14 +321,18 @@ describe('DatasetController', () => {
313321
)
314322
.send({liked: false, comment: 'Not good'})
315323
.expect(204);
316-
const dataset = await repo.findById(dummyDataset.id, {
317-
include: ['actions'],
318-
});
324+
const dataset = await repo.findById(
325+
dummyDataset.id,
326+
{
327+
include: ['actions'],
328+
},
329+
{skipUserFilter: true},
330+
);
319331
expect(dataset).to.have.property('votes', 0);
320332
expect(dataset.actions).to.be.Array();
321333
expect(dataset.actions).to.have.length(2);
322334
});
323-
it('should handle muliple dislikes only', async () => {
335+
it('should handle muliple dislikes from different users', async () => {
324336
await client
325337
.patch(`/datasets/${dummyDataset.id}`)
326338
.set(
@@ -338,9 +350,13 @@ describe('DatasetController', () => {
338350
)
339351
.send({liked: false, comment: 'Not good'})
340352
.expect(204);
341-
const dataset = await repo.findById(dummyDataset.id, {
342-
include: ['actions'],
343-
});
353+
const dataset = await repo.findById(
354+
dummyDataset.id,
355+
{
356+
include: ['actions'],
357+
},
358+
{skipUserFilter: true},
359+
);
344360
expect(dataset).to.have.property('votes', -2);
345361
expect(dataset.actions).to.be.Array();
346362
expect(dataset.actions).to.have.length(2);
@@ -396,7 +412,7 @@ describe('DatasetController', () => {
396412
const ctx = new Context(appInstance);
397413
ctx.bind(AuthenticationBindings.CURRENT_USER).to({
398414
id: 'test-user',
399-
userTenantId: 'default',
415+
userTenantId: 'default-user-id',
400416
} as unknown as IAuthUserWithPermissions);
401417
const dsrepo = await ctx.get<DataSetRepository>(
402418
`repositories.${DataSetRepository.name}`,

src/__tests__/test-helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ export async function seedDataset(appInstance: TestApp) {
189189
});
190190
}
191191

192-
export function buildToken(permissions: string[], userTenantId = 'default') {
192+
export function buildToken(
193+
permissions: string[],
194+
userTenantId = 'default-user-id',
195+
) {
193196
return sign(
194197
{
195198
id: 'test-user',
@@ -248,7 +251,8 @@ export async function getRepo(app: Application, repo: string) {
248251
const ctx = new Context(app);
249252
ctx.bind(AuthenticationBindings.CURRENT_USER).to({
250253
id: 'test-user',
251-
userTenantId: 'default',
254+
userTenantId: 'default-user-id',
255+
role: 'admin',
252256
} as unknown as IAuthUserWithPermissions);
253257
return ctx.get<DataSetRepository>(`repositories.${DataSetRepository.name}`);
254258
}

src/components/db-query/repositories/dataset-action.repository.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ export class DatasetActionRepository extends DefaultTransactionalRepository<
2828
options?: Options,
2929
): Promise<DatasetAction[]> {
3030
const user = await this.getCurrentUser();
31+
if (options?.skipUserFilter === true) {
32+
return super.find(filter, options);
33+
}
3134
filter = filter ?? {};
3235
filter.where = {
33-
and: [filter.where ?? {}, {userId: user.tenantId}],
36+
and: [filter.where ?? {}, {userId: user.userTenantId}],
3437
};
3538
return super.find(filter, options);
3639
}

0 commit comments

Comments
 (0)