|
| 1 | +/** |
| 2 | + * Created by championswimmer on 16/05/17. |
| 3 | + */ |
| 4 | +const db = require('./db') |
| 5 | +const fs = require('fs') |
| 6 | + |
| 7 | +function getClaims(options) { |
| 8 | + const offset = (options.page - 1) * options.size |
| 9 | + |
| 10 | + var whereClause = { status: options.status } |
| 11 | + if (options.username) { |
| 12 | + whereClause = { status: options.status, user: options.username } |
| 13 | + } else if (options.projectname) { |
| 14 | + whereClause = { status: options.status, repo: options.projectname } |
| 15 | + } else if (options.minbounty && options.maxbounty) { |
| 16 | + whereClause = { status: options.status, bounty: { $between: [options.minbounty, options.maxbounty] } } |
| 17 | + } |
| 18 | + |
| 19 | + const distinctUsers = db.Claim.aggregate('user', 'DISTINCT', { plain: false, where: { status: options.status } }) |
| 20 | + const distinctProjects = db.Claim.aggregate('repo', 'DISTINCT', { plain: false, where: { status: options.status } }) |
| 21 | + const allClaims = db.Claim.findAndCountAll({ |
| 22 | + limit: options.size, |
| 23 | + offset: offset, |
| 24 | + where: whereClause, |
| 25 | + order: [['updatedAt', 'DESC']] |
| 26 | + }) |
| 27 | + |
| 28 | + return Promise.all([distinctUsers, allClaims, distinctProjects]) |
| 29 | +} |
| 30 | + |
| 31 | +function getClaimById(claimId) { |
| 32 | + return db.Claim.findById(claimId) |
| 33 | +} |
| 34 | + |
| 35 | +function delClaim(claimId) { |
| 36 | + if (isNaN(+claimId)) { |
| 37 | + return res.send('ClaimId must be a number') |
| 38 | + } |
| 39 | + return db.Claim.destroy({ |
| 40 | + where: { |
| 41 | + id: claimId |
| 42 | + } |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +function updateClaim(claimId, { status, reason, bounty }) { |
| 47 | + const claim = { |
| 48 | + action: 'update', |
| 49 | + claimId, |
| 50 | + status, |
| 51 | + bounty |
| 52 | + } |
| 53 | + fs.writeFile(__dirname + '/../audit/' + new Date().toISOString() + '.json', JSON.stringify(claim), () => {}) |
| 54 | + |
| 55 | + return db.Claim.update( |
| 56 | + { |
| 57 | + status: status, |
| 58 | + reason: reason, |
| 59 | + bounty: bounty |
| 60 | + }, |
| 61 | + { |
| 62 | + where: { |
| 63 | + id: claimId |
| 64 | + }, |
| 65 | + returning: true |
| 66 | + } |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +function createClaim(user, issueUrl, pullUrl, bounty, status) { |
| 71 | + const claim = { |
| 72 | + action: 'create', |
| 73 | + user, |
| 74 | + issueUrl, |
| 75 | + pullUrl, |
| 76 | + bounty, |
| 77 | + status |
| 78 | + } |
| 79 | + fs.writeFile(__dirname + '/../audit/' + new Date().toISOString() + '.json', JSON.stringify(claim), () => {}) |
| 80 | + |
| 81 | + return db.Claim.create({ |
| 82 | + user, |
| 83 | + issueUrl, |
| 84 | + pullUrl, |
| 85 | + repo: pullUrl.split('github.com/')[1].split('/')[1], |
| 86 | + bounty: bounty, |
| 87 | + status: status |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +function getLeaderboard(options = {}) { |
| 92 | + options.size = parseInt(options.size || 0) |
| 93 | + const offset = (options.page - 1) * options.size |
| 94 | + |
| 95 | + const userCount = db.Claim.aggregate('user', 'count', { distinct: true }) |
| 96 | + |
| 97 | + const results = db.Database.query(`SELECT "user", |
| 98 | + SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) as "bounty", |
| 99 | + COUNT("bounty") as "pulls" |
| 100 | + FROM "claims" AS "claim" |
| 101 | + GROUP BY "user" |
| 102 | + ORDER BY SUM(CASE WHEN "claim"."status" = 'accepted' THEN "bounty" ELSE 0 END) DESC, COUNT("bounty") DESC |
| 103 | + LIMIT ${options.size} OFFSET ${offset}`) |
| 104 | + |
| 105 | + return Promise.all([userCount, results]) |
| 106 | +} |
| 107 | + |
| 108 | +function getCounts() { |
| 109 | + const participants = db.Claim.aggregate('user', 'count', { distinct: true }) |
| 110 | + const claims = db.Claim.aggregate('*', 'count') |
| 111 | + var accepted = db.Claim.aggregate('bounty', 'sum', { |
| 112 | + where: { |
| 113 | + status: 'accepted' |
| 114 | + } |
| 115 | + }) |
| 116 | + var totalclaimed = db.Claim.aggregate('bounty', 'sum') |
| 117 | + |
| 118 | + var filterNaN = data => data || 0 |
| 119 | + |
| 120 | + var counts = Promise.all([participants, claims, accepted, totalclaimed]).then(values => values.map(filterNaN)) |
| 121 | + |
| 122 | + return counts |
| 123 | +} |
| 124 | + |
| 125 | +module.exports = { |
| 126 | + getClaims, |
| 127 | + delClaim, |
| 128 | + createClaim, |
| 129 | + getLeaderboard, |
| 130 | + getClaimById, |
| 131 | + updateClaim, |
| 132 | + getCounts |
| 133 | +} |
0 commit comments