2020-05-22 19:58:01 +03:00
|
|
|
import faker from 'faker';
|
|
|
|
import moment from 'moment';
|
2020-06-19 20:14:14 +03:00
|
|
|
import {Response} from 'ember-cli-mirage';
|
2022-01-19 16:15:19 +03:00
|
|
|
import {extractFilterParam, paginateModelCollection} from '../utils';
|
2021-04-08 14:06:27 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2019-01-22 20:18:44 +03:00
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
export function mockMembersStats(server) {
|
2021-04-06 12:08:45 +03:00
|
|
|
server.get('/members/stats/count', function (db, {queryParams}) {
|
2020-05-22 19:58:01 +03:00
|
|
|
let {days} = queryParams;
|
|
|
|
|
2021-04-12 17:32:39 +03:00
|
|
|
let firstSubscriberDays = faker.datatype.number({min: 30, max: 600});
|
2020-05-22 19:58:01 +03:00
|
|
|
|
|
|
|
if (days === 'all-time') {
|
|
|
|
days = firstSubscriberDays;
|
|
|
|
} else {
|
|
|
|
days = Number(days);
|
|
|
|
}
|
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
if (firstSubscriberDays > days) {
|
2021-04-12 17:32:39 +03:00
|
|
|
total += faker.datatype.number({max: 1000});
|
2020-05-22 19:58:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// simulate sql GROUP BY where days with 0 subscribers are missing
|
|
|
|
let dateCounts = {};
|
|
|
|
let i = 0;
|
|
|
|
while (i < days) {
|
|
|
|
let date = moment().subtract(i, 'days').format('YYYY-MM-DD');
|
2021-04-12 17:32:39 +03:00
|
|
|
let count = faker.datatype.number({min: 0, max: 30});
|
2020-05-22 19:58:01 +03:00
|
|
|
|
|
|
|
if (count !== 0) {
|
|
|
|
dateCounts[date] = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// similar to what we'll need to do on the server
|
|
|
|
let totalOnDate = {};
|
|
|
|
let j = days - 1;
|
|
|
|
while (j >= 0) {
|
|
|
|
let date = moment().subtract(j, 'days').format('YYYY-MM-DD');
|
|
|
|
totalOnDate[date] = total + (dateCounts[date] || 0);
|
|
|
|
total += (dateCounts[date] || 0);
|
|
|
|
j -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-05-25 12:35:39 +03:00
|
|
|
total,
|
2021-04-06 12:08:45 +03:00
|
|
|
resource: 'members',
|
|
|
|
data: Object.keys(totalOnDate).map((key, idx, arr) => {
|
|
|
|
return {
|
|
|
|
date: key,
|
|
|
|
free: arr[key],
|
|
|
|
paid: 0,
|
|
|
|
comped: 0
|
|
|
|
};
|
|
|
|
})
|
2020-05-22 19:58:01 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-22 20:18:44 +03:00
|
|
|
export default function mockMembers(server) {
|
2019-11-28 14:30:21 +03:00
|
|
|
server.post('/members/', function ({members}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
|
|
|
|
return members.create(Object.assign({}, attrs, {id: 99}));
|
|
|
|
});
|
|
|
|
|
2021-04-08 14:06:27 +03:00
|
|
|
server.get('/members/', function ({members}, {queryParams}) {
|
2022-02-14 19:23:50 +03:00
|
|
|
let {filter, search, page, limit} = queryParams;
|
2021-04-08 14:06:27 +03:00
|
|
|
|
|
|
|
page = +page || 1;
|
|
|
|
limit = +limit || 15;
|
|
|
|
|
2022-02-14 19:23:50 +03:00
|
|
|
const labelFilter = extractFilterParam('label', filter);
|
2021-04-08 14:06:27 +03:00
|
|
|
|
|
|
|
let collection = members.all().filter((member) => {
|
|
|
|
let matchesLabel = true;
|
|
|
|
|
|
|
|
if (!isEmpty(labelFilter)) {
|
|
|
|
matchesLabel = false;
|
|
|
|
|
|
|
|
labelFilter.forEach((slug) => {
|
|
|
|
if (member.labels.models.find(l => l.slug === slug)) {
|
|
|
|
matchesLabel = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchesLabel;
|
|
|
|
});
|
|
|
|
|
2022-02-14 19:23:50 +03:00
|
|
|
if (search) {
|
|
|
|
const query = search.toLowerCase();
|
|
|
|
|
|
|
|
collection = collection.filter((member) => {
|
|
|
|
return member.name.toLowerCase().indexOf(query) !== -1
|
|
|
|
|| member.email.toLowerCase().indexOf(query) !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:06:27 +03:00
|
|
|
return paginateModelCollection('members', collection, page, limit);
|
|
|
|
});
|
2019-02-22 14:31:45 +03:00
|
|
|
|
2020-06-19 20:14:14 +03:00
|
|
|
server.del('/members/', function ({members}, {queryParams}) {
|
2021-04-08 14:06:27 +03:00
|
|
|
if (!queryParams.filter && !queryParams.search && queryParams.all !== 'true') {
|
2020-06-19 20:14:14 +03:00
|
|
|
return new Response(422, {}, {errors: [{
|
|
|
|
type: 'IncorrectUsageError',
|
|
|
|
message: 'DELETE /members/ must be used with a filter, search, or all=true query parameter'
|
|
|
|
}]});
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:06:27 +03:00
|
|
|
let membersToDelete = members.all();
|
|
|
|
|
|
|
|
if (queryParams.filter) {
|
|
|
|
let labelFilter = extractFilterParam('label', queryParams.filter);
|
|
|
|
|
|
|
|
membersToDelete = membersToDelete.filter((member) => {
|
|
|
|
let matches = false;
|
|
|
|
labelFilter.forEach((slug) => {
|
|
|
|
if (member.labels.models.find(l => l.slug === slug)) {
|
|
|
|
matches = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return matches;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = membersToDelete.length;
|
|
|
|
membersToDelete.destroy();
|
2020-06-19 20:14:14 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
stats: {
|
2021-04-08 14:06:27 +03:00
|
|
|
successful: count
|
2020-06-19 20:14:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-02-22 14:31:45 +03:00
|
|
|
server.get('/members/:id/', function ({members}, {params}) {
|
|
|
|
let {id} = params;
|
|
|
|
let member = members.find(id);
|
|
|
|
|
|
|
|
return member || new Response(404, {}, {
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'NotFoundError',
|
2019-02-22 14:31:45 +03:00
|
|
|
message: 'Member not found.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-28 14:30:21 +03:00
|
|
|
server.put('/members/:id/');
|
|
|
|
|
2019-02-22 14:31:45 +03:00
|
|
|
server.del('/members/:id/');
|
2020-05-22 19:58:01 +03:00
|
|
|
|
2021-04-08 18:06:00 +03:00
|
|
|
server.get('/members/upload/', function () {
|
|
|
|
return new Response(200, {
|
|
|
|
'Content-Disposition': 'attachment',
|
|
|
|
filename: `members.${moment().format('YYYY-MM-DD')}.csv`,
|
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
}, '');
|
|
|
|
});
|
|
|
|
|
2022-02-11 14:07:13 +03:00
|
|
|
server.post('/members/upload/', function ({labels}, request) {
|
|
|
|
const label = labels.create();
|
|
|
|
|
|
|
|
// TODO: parse CSV and create member records
|
|
|
|
for (const kvPair of request.requestBody.entries()) {
|
|
|
|
const [key, value] = kvPair;
|
|
|
|
console.log({key, value}); // eslint-disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response(201, {}, {
|
|
|
|
meta: {
|
|
|
|
import_label: label,
|
|
|
|
stats: {imported: 1, invalid: []}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-19 15:57:58 +03:00
|
|
|
server.get('/members/events/', function ({memberActivityEvents}, {queryParams}) {
|
|
|
|
let {limit} = queryParams;
|
|
|
|
|
|
|
|
limit = +limit || 15;
|
|
|
|
|
|
|
|
let collection = memberActivityEvents.all().sort((a, b) => {
|
|
|
|
return (new Date(a.createdAt)) - (new Date(b.createdAt));
|
|
|
|
}).slice(0, limit);
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
});
|
|
|
|
|
2020-05-22 19:58:01 +03:00
|
|
|
mockMembersStats(server);
|
2019-01-22 20:18:44 +03:00
|
|
|
}
|