mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 22:27:26 +01:00
Deprecate API v2 (#7761)
* deprecate api-v2 * remove v2 test helpers * remove unused string
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import mongoose from 'mongoose';
|
||||
import Bluebird from 'bluebird';
|
||||
import validator from 'validator';
|
||||
import baseModel from '../libs/api-v3/baseModel';
|
||||
import baseModel from '../libs/baseModel';
|
||||
import _ from 'lodash';
|
||||
import * as Tasks from './task';
|
||||
import { model as User } from './user';
|
||||
@@ -9,10 +9,10 @@ import {
|
||||
model as Group,
|
||||
TAVERN_ID,
|
||||
} from './group';
|
||||
import { removeFromArray } from '../libs/api-v3/collectionManipulators';
|
||||
import { removeFromArray } from '../libs/collectionManipulators';
|
||||
import shared from '../../../common';
|
||||
import { sendTxn as txnEmail } from '../libs/api-v3/email';
|
||||
import sendPushNotification from '../libs/api-v3/pushNotifications';
|
||||
import { sendTxn as txnEmail } from '../libs/email';
|
||||
import sendPushNotification from '../libs/pushNotifications';
|
||||
import cwait from 'cwait';
|
||||
|
||||
const Schema = mongoose.Schema;
|
||||
@@ -332,105 +332,4 @@ schema.methods.closeChal = async function closeChal (broken = {}) {
|
||||
Bluebird.all(backgroundTasks);
|
||||
};
|
||||
|
||||
// Methods to adapt the new schema to API v2 responses (mostly tasks inside the challenge model)
|
||||
// These will be removed once API v2 is discontinued
|
||||
|
||||
// Get all the tasks belonging to a challenge,
|
||||
schema.methods.getTasks = function getChallengeTasks () {
|
||||
let args = Array.from(arguments);
|
||||
let cb;
|
||||
let type;
|
||||
|
||||
if (args.length === 1) {
|
||||
cb = args[0];
|
||||
} else if (args.length > 1) {
|
||||
type = args[0];
|
||||
cb = args[1];
|
||||
} else {
|
||||
cb = function noop () {};
|
||||
}
|
||||
|
||||
let query = {
|
||||
userId: {
|
||||
$exists: false,
|
||||
},
|
||||
|
||||
'challenge.id': this._id,
|
||||
};
|
||||
|
||||
if (type) query.type = type;
|
||||
|
||||
return Tasks.Task.find(query, cb); // so we can use it as a promise
|
||||
};
|
||||
|
||||
// Given challenge and an array of tasks and one of members return an API compatible challenge + tasks obj + members
|
||||
schema.methods.addToChallenge = function addToChallenge (tasks, members) {
|
||||
let obj = this.toJSON();
|
||||
obj.members = members;
|
||||
|
||||
let tasksOrder = obj.tasksOrder; // Saving a reference because we won't return it
|
||||
|
||||
obj.habits = [];
|
||||
obj.dailys = [];
|
||||
obj.todos = [];
|
||||
obj.rewards = [];
|
||||
|
||||
obj.tasksOrder = undefined;
|
||||
let unordered = [];
|
||||
|
||||
tasks.forEach((task) => {
|
||||
// We want to push the task at the same position where it's stored in tasksOrder
|
||||
let pos = tasksOrder[`${task.type}s`].indexOf(task._id);
|
||||
if (pos === -1) { // Should never happen, it means the lists got out of sync
|
||||
unordered.push(task.toJSONV2());
|
||||
} else {
|
||||
obj[`${task.type}s`][pos] = task.toJSONV2();
|
||||
}
|
||||
});
|
||||
|
||||
// Reconcile unordered items
|
||||
unordered.forEach((task) => {
|
||||
obj[`${task.type}s`].push(task);
|
||||
});
|
||||
|
||||
// Remove null values that can be created when inserting tasks at an index > length
|
||||
['habits', 'dailys', 'rewards', 'todos'].forEach((type) => {
|
||||
obj[type] = _.compact(obj[type]);
|
||||
});
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
// Return the data maintaining backward compatibility
|
||||
schema.methods.getTransformedData = function getTransformedData (options) {
|
||||
let self = this;
|
||||
|
||||
let cb = options.cb;
|
||||
let populateMembers = options.populateMembers;
|
||||
|
||||
let queryMembers = {
|
||||
challenges: self._id,
|
||||
};
|
||||
|
||||
let selectDataMembers = '_id';
|
||||
|
||||
if (populateMembers) {
|
||||
selectDataMembers += ` ${populateMembers}`;
|
||||
}
|
||||
|
||||
let membersQuery = User.find(queryMembers).select(selectDataMembers);
|
||||
if (options.limitPopulation) membersQuery.limit(15);
|
||||
|
||||
Bluebird.all([
|
||||
membersQuery.exec(),
|
||||
self.getTasks(),
|
||||
])
|
||||
.then((results) => {
|
||||
cb(null, self.addToChallenge(results[1], results[0]));
|
||||
})
|
||||
.catch(cb);
|
||||
};
|
||||
|
||||
// END of API v2 methods
|
||||
|
||||
export let model = mongoose.model('Challenge', schema);
|
||||
|
||||
Reference in New Issue
Block a user