2018-08-15 13:19:06 +00:00
|
|
|
const Router = require('koa-router')
|
|
|
|
const {
|
|
|
|
genError,
|
|
|
|
checkSession,
|
2018-08-16 08:49:54 +00:00
|
|
|
resObject,
|
|
|
|
chkObject,
|
|
|
|
toInt
|
2018-08-15 13:19:06 +00:00
|
|
|
} = require('@libs/route-utils')
|
|
|
|
const r = new Router()
|
2018-08-16 08:49:54 +00:00
|
|
|
const evt = require('@root/event')
|
2018-08-16 16:50:13 +00:00
|
|
|
const koaBody = require('koa-body')
|
2018-08-15 13:19:06 +00:00
|
|
|
|
|
|
|
const typeTwitch = async (c, n) => {
|
|
|
|
let text = `select * from "public"."twitch_channel" where "id" = $1`
|
|
|
|
let values = [c.session.user.id]
|
|
|
|
let result = await c.db.query({text, values})
|
|
|
|
c.state.channelList = result.rows
|
|
|
|
return n()
|
|
|
|
}
|
|
|
|
|
|
|
|
const typeSystem = async (c, n) => {
|
2018-08-22 12:14:23 +00:00
|
|
|
let text = `select * from "public"."twitch_channel" order by "name" asc`
|
2018-08-20 15:07:43 +00:00
|
|
|
let values = []
|
2018-08-15 13:19:06 +00:00
|
|
|
let result = await c.db.query({text, values})
|
|
|
|
c.state.channelList = result.rows
|
|
|
|
return n()
|
|
|
|
}
|
|
|
|
|
2018-08-16 08:49:54 +00:00
|
|
|
const getChannelList = async (c, n) => {
|
2018-08-15 13:19:06 +00:00
|
|
|
if (c.session.loginType === 'twitch') {
|
|
|
|
return typeTwitch(c, n)
|
|
|
|
} else if (c.session.loginType === 'system') {
|
|
|
|
return typeSystem(c, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
throw genError('Forbidden')
|
2018-08-16 08:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const hasChannel = (key = '') => {
|
|
|
|
if (typeof key !== 'string' || key.trim().length === 0) key = 'chid'
|
|
|
|
key = key.trim()
|
|
|
|
return async (c, n) => {
|
|
|
|
if (!('channelList' in c.state) || !Array.isArray(c.state.channelList)) throw genError('InternalError')
|
|
|
|
let chid = c.params[key] || ''
|
|
|
|
c.state.chid = chid
|
|
|
|
let chk = chkObject.bind({body: {chid}})
|
|
|
|
if (!chk('chid', 'string')) throw genError('DataFormat')
|
|
|
|
|
|
|
|
if (c.state.channelList.length === 0) throw genError('NotFound', 'channel not found')
|
|
|
|
let channel = c.state.channelList.filter(t => t.id === chid)
|
|
|
|
if (channel.length === 0) throw genError('NotFound', 'channel not found')
|
|
|
|
|
|
|
|
c.state.channel = channel[0]
|
|
|
|
return n()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.get('/channels', checkSession, getChannelList, async (c, n) => {
|
2018-08-15 13:19:06 +00:00
|
|
|
if (!('channelList' in c.state) || !Array.isArray(c.state.channelList)) throw genError('InternalError')
|
|
|
|
|
|
|
|
c.obj = resObject('Success', {
|
|
|
|
list: c.state.channelList
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-16 08:49:54 +00:00
|
|
|
r.get('/channel/:chid', checkSession, getChannelList, hasChannel('chid'), async (c, n) => {
|
|
|
|
c.obj = resObject('Success', {channel: c.state.channel})
|
|
|
|
})
|
|
|
|
|
2018-08-21 15:32:43 +00:00
|
|
|
r.put('/channel/:chid/botjoin', checkSession, getChannelList, hasChannel('chid'), koaBody(), async (c, n) => {
|
2018-08-16 08:49:54 +00:00
|
|
|
if (!c.chkBody('join', 'number')) throw genError('DataFormat')
|
|
|
|
let join = toInt(c.request.body.join, 0, 0, 1)
|
|
|
|
let text = `update "public"."twitch_channel" set "join" = $1 where "id" = $2`
|
|
|
|
let values = [!!join, c.state.channel.id]
|
|
|
|
await c.db.query({
|
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
|
|
|
|
|
|
|
evt.emit(join ? 'twitchJoin' : 'twitchLeave', c.state.channel.name)
|
|
|
|
|
|
|
|
c.obj = resObject('Success')
|
|
|
|
})
|
|
|
|
|
2018-08-21 15:32:43 +00:00
|
|
|
r.put('/channel/:chid/opay', checkSession, getChannelList, hasChannel('chid'), koaBody(), async (c, n) => {
|
2018-08-16 08:49:54 +00:00
|
|
|
if (!c.chkBody('opay', 'string', true)) throw genError('DataFormat')
|
|
|
|
|
|
|
|
let text = `update "public"."twitch_channel" set "opayid" = $1 where "id" = $2`
|
|
|
|
let values = [c.request.body.opay, c.state.channel.id]
|
|
|
|
await c.db.query({
|
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
|
|
|
|
|
|
|
c.obj = resObject('Success')
|
|
|
|
})
|
|
|
|
|
2018-08-16 16:50:13 +00:00
|
|
|
r.get('/channel/:chid/opay/setting', checkSession, getChannelList, hasChannel('chid'), async (c, n) => {
|
2018-08-16 08:49:54 +00:00
|
|
|
let text = `select ds.* from "public"."donate_setting" ds
|
|
|
|
left join "public"."twitch_channel" tw
|
|
|
|
on tw.id = ds.twitch
|
|
|
|
where
|
2018-08-16 16:50:13 +00:00
|
|
|
tw.id = $1
|
|
|
|
and tw.opayid != ''
|
|
|
|
limit 1`
|
2018-08-16 08:49:54 +00:00
|
|
|
let values = [c.state.channel.id]
|
|
|
|
let result = await c.db.query({
|
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
2018-08-22 14:11:43 +00:00
|
|
|
|
2018-08-16 08:49:54 +00:00
|
|
|
c.obj = resObject('Success', {
|
2018-08-22 14:11:43 +00:00
|
|
|
setting: result.rowCount === 0 ? {} : result.rows[0]
|
2018-08-16 08:49:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-16 16:50:13 +00:00
|
|
|
r.put('/channel/:chid/opay/setting', checkSession, getChannelList, hasChannel('chid'), koaBody(), async (c, n) => {
|
2018-08-26 13:27:36 +00:00
|
|
|
if (!c.chkBody('end', 'number') || !c.chkBody('title', 'string') || !c.chkBody('amount', 'number')) throw genError('DataFormat')
|
2018-08-16 16:50:13 +00:00
|
|
|
|
|
|
|
let ets = toInt(c.request.body.end, 0, 0)
|
|
|
|
let amount = toInt(c.request.body.amount, 0, 0)
|
2018-08-26 13:27:36 +00:00
|
|
|
let startAmount = toInt(c.request.body.start_amount, 0, 0)
|
2018-08-16 16:50:13 +00:00
|
|
|
let ed = new Date(ets)
|
|
|
|
|
2018-08-26 13:27:36 +00:00
|
|
|
let text = `insert into "public"."donate_setting"
|
|
|
|
("twitch", "start_date", "end_date", "target_amount", "title", "start_amount") values
|
|
|
|
($1, now(), $2, $3, $4, $5)
|
2018-08-16 16:50:13 +00:00
|
|
|
on CONFLICT ("twitch") DO UPDATE
|
|
|
|
set
|
2018-08-26 13:27:36 +00:00
|
|
|
"start_date" = now(),
|
|
|
|
"end_date" = $2,
|
|
|
|
"target_amount" = $3,
|
|
|
|
"title" = $4,
|
|
|
|
"start_amount" = $5`
|
|
|
|
let values = [c.state.channel.id, ed, amount, c.request.body.title, startAmount]
|
2018-08-22 16:23:16 +00:00
|
|
|
await c.db.query({
|
2018-08-16 16:50:13 +00:00
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
|
|
|
|
|
|
|
c.obj = resObject('Success')
|
|
|
|
})
|
|
|
|
|
2018-08-23 15:10:45 +00:00
|
|
|
r.get('/channel/:chid/opay/bar', async (c, n) => {
|
|
|
|
let chid = c.params.chid || ''
|
|
|
|
if (typeof chid !== 'string' || chid.trim().length === 0) throw genError('DataFormat')
|
|
|
|
let text = `select ds.* from "public"."donate_setting" ds
|
|
|
|
left join "public"."twitch_channel" tw
|
|
|
|
on tw.id = ds.twitch
|
|
|
|
where
|
|
|
|
tw.opayid != ''
|
|
|
|
and tw.id = $1`
|
|
|
|
let values = [chid]
|
|
|
|
|
|
|
|
let result = await c.db.query({
|
|
|
|
text,
|
|
|
|
values
|
|
|
|
})
|
|
|
|
let setting = {}
|
|
|
|
if (result.rowCount === 0) setting = {}
|
|
|
|
setting = result.rows[0]
|
2018-08-26 13:27:36 +00:00
|
|
|
setting.total = toInt(setting.start_amount, 0, 0)
|
2018-08-23 15:10:45 +00:00
|
|
|
|
2018-08-23 15:21:49 +00:00
|
|
|
if (result.rowCount > 0) {
|
|
|
|
text = `select sum(list.price) as total from "public"."opay_donate_list" list
|
2018-08-23 15:26:07 +00:00
|
|
|
left join "public"."twitch_channel" tw
|
2018-08-23 15:21:49 +00:00
|
|
|
on tw.opayid = list.opayid
|
|
|
|
where
|
|
|
|
tw.opayid != ''
|
|
|
|
and tw.id = $1
|
2018-08-23 15:33:35 +00:00
|
|
|
and list.ctime >= $2
|
2018-08-23 15:21:49 +00:00
|
|
|
group by list.opayid`
|
|
|
|
values = [chid, setting.start_date]
|
|
|
|
let priceResult = await c.db.query({
|
|
|
|
text, values
|
|
|
|
})
|
|
|
|
if (priceResult.rowCount > 0) {
|
2018-08-23 15:39:53 +00:00
|
|
|
setting.total = priceResult.rows[0].total
|
2018-08-23 15:21:49 +00:00
|
|
|
} else {
|
|
|
|
setting.total = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:10:45 +00:00
|
|
|
c.obj = resObject('Success', {setting})
|
|
|
|
})
|
|
|
|
|
2018-08-15 13:19:06 +00:00
|
|
|
module.exports = r
|