Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Change NPS Vote identifier + nps index to unique #25423

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/meteor/app/models/server/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export class BaseRaw<T, C extends DefaultFields<T> = undefined> implements IBase

const indexes = this.modelIndexes();
if (indexes?.length) {
this.col.createIndexes(indexes);
this.col.createIndexes(indexes).catch((e) => {
console.warn(`Error creating indexes for ${this.name}`, e);
});
}

this.preventSetUpdatedAt = options?.preventSetUpdatedAt ?? false;
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/server/raw/NpsVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BaseRaw, IndexSpecification } from './BaseRaw';
type T = INpsVote;
export class NpsVoteRaw extends BaseRaw<T> {
modelIndexes(): IndexSpecification[] {
return [{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 } }];
return [{ key: { npsId: 1, status: 1, sentAt: 1 } }, { key: { npsId: 1, identifier: 1 }, unique: true }];
}

findNotSentByNpsId(npsId: string, options?: WithoutProjection<FindOneOptions<T>>): Cursor<T> {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/startup/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ import './v258';
import './v259';
import './v260';
import './v261';
import './v262';
import './xrun';
41 changes: 41 additions & 0 deletions apps/meteor/server/startup/migrations/v262.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MongoInternals } from 'meteor/mongo';

import { addMigration } from '../../lib/migrations';

addMigration({
version: 262,
async up() {
const { mongo } = MongoInternals.defaultRemoteCollectionDriver();
const npsVote = mongo.db.collection('rocketchat_nps_vote');

const duplicated = await npsVote
.aggregate([
{
$group: {
_id: { identifier: '$identifier', npsId: '$npsId' },
total: { $sum: 1 },
firstId: { $first: '$_id' },
},
},
{
$match: {
total: { $gt: 1 },
},
},
])
.toArray(); // since there should not be too much duplicated, it is safe to use .toArray()

await Promise.all(
duplicated.map((record) =>
npsVote.deleteMany({
_id: { $ne: record.firstId },
identifier: record._id.identifier,
npsId: record._id.npsId,
}),
),
);

await npsVote.dropIndex('npsId_1_identifier_1');
await npsVote.createIndex({ npsId: 1, identifier: 1 }, { unique: true });
},
});