Added code to update user tag list along with the existing code that already involves updating the user documents of challenge members (#12312)

* Piggybacking the updating of user tag list.

When a new task is being added to a challenge, added code to update user tag list along with the existing code that already involves syncing / updating the user documents of challenge members.

* Update comment on number of simulatenaeous users to be updated concurrently in TaskQueue.

* Added comment to explain previous commit caaca469f8 (Update comment on number of simulateneous users to be updated concurrently in TaskQueue)

* Added unit tests for testing commit caaca469f8 (Update comment on number of simulateneous users to be updated concurrently in TaskQueue)

* Removed unused lines from newly added test cases

* Implemented lint suggestions

* Update code with changes requested in PR
This commit is contained in:
tsukimi2
2020-07-04 03:55:59 +08:00
committed by GitHub
parent 453d60b5bf
commit a388abc124
2 changed files with 97 additions and 3 deletions

View File

@@ -35,6 +35,33 @@ describe('Challenge Model', () => {
notes: 'test notes',
},
};
const tasks2ToTest = {
habit: {
text: 'test habit 2',
type: 'habit',
up: false,
down: true,
notes: 'test notes',
},
todo: {
text: 'test todo 2',
type: 'todo',
notes: 'test notes',
},
daily: {
text: 'test daily 2',
type: 'daily',
frequency: 'daily',
everyX: 5,
startDate: new Date(),
notes: 'test notes',
},
reward: {
text: 'test reward 2',
type: 'reward',
notes: 'test notes',
},
};
beforeEach(async () => {
guild = new Group({
@@ -146,6 +173,60 @@ describe('Challenge Model', () => {
expect(syncedTask.attribute).to.eql('str');
});
it('should add challenge tag back to user upon syncing challenge tasks to a user with challenge tag removed', async () => {
await challenge.addTasks([task]);
const newMember = new User({
guilds: [guild._id],
});
await newMember.save();
await challenge.syncTasksToUser(newMember);
let updatedNewMember = await User.findById(newMember._id).exec();
const updatedNewMemberId = updatedNewMember._id;
updatedNewMember.tags = [];
await updatedNewMember.save();
const taskValue2 = tasks2ToTest[taskType];
const task2 = new Tasks[`${taskType}`](Tasks.Task.sanitize(taskValue2));
task2.challenge.id = challenge._id;
await challenge.addTasks([task2]);
await challenge.syncTasksToUser(updatedNewMember);
updatedNewMember = await User.findById(updatedNewMemberId).exec();
expect(updatedNewMember.tags.length).to.equal(1);
expect(updatedNewMember.tags[0].id).to.equal(challenge._id);
expect(updatedNewMember.tags[0].name).to.equal(challenge.shortName);
});
it('should not add a duplicate challenge tag to user upon syncing challenge tasks to a user with existing challenge tag', async () => {
await challenge.addTasks([task]);
const newMember = new User({
guilds: [guild._id],
});
await newMember.save();
await challenge.syncTasksToUser(newMember);
let updatedNewMember = await User.findById(newMember._id).exec();
const updatedNewMemberId = updatedNewMember._id;
const taskValue2 = tasks2ToTest[taskType];
const task2 = new Tasks[`${taskType}`](Tasks.Task.sanitize(taskValue2));
task2.challenge.id = challenge._id;
await challenge.addTasks([task2]);
await challenge.syncTasksToUser(updatedNewMember);
updatedNewMember = await User.findById(updatedNewMemberId);
expect(updatedNewMember.tags.length).to.equal(8);
expect(updatedNewMember.tags[7].id).to.equal(challenge._id);
expect(updatedNewMember.tags[7].name).to.equal(challenge.shortName);
expect(updatedNewMember.tags.filter(tag => tag.id === challenge._id).length).to.equal(1);
});
it('syncs challenge tasks to a user with the existing task', async () => {
await challenge.addTasks([task]);