facebook page notify v1.0

This commit is contained in:
Jay
2018-06-27 23:38:56 +08:00
parent 99e5a455cb
commit e2eeef9d82
4 changed files with 190 additions and 154 deletions
+44 -35
View File
@@ -36,7 +36,7 @@ const addGroup = async (txt, source = {}, db) => {
/**
* add facebook page notify to group
* @param {string} txt command body format => pageid name tmpl
* @param {string} txt command body format => pageid tmpl
* @param {object} source
* @param {object} db
*/
@@ -47,8 +47,7 @@ const addPage = async (txt, source = {}, db) => {
let arr = txt.split(' ')
if (arr.length < 3) return null
let page = arr[0]
let name = arr[1]
let tmpl = arr.slice(2).join(' ')
let tmpl = arr.slice(1).join(' ')
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
let values = [groupId]
let result = await db.query({
@@ -63,43 +62,53 @@ const addPage = async (txt, source = {}, db) => {
let reply = 'not owner'
return { reply }
}
// check pageid in group
text = `select fb."id" from "public"."facebook_page" fb
left join "public"."line_group" line
on fb."groupid" = line."id"
where
fb."pageid" = $1`
// check page exists
text = `select "id" from "public"."facebook_page" where "id"=$1`
values = [page]
result = await db.query({
let fb = await db.query({
text,
values
})
if (result.rowCount > 0) {
let reply = 'page exists'
return { reply }
// if no page data insert page data and insert rt
if (fb.rowCount === 0) {
text = `insert into "public"."facebook_page" ("id", "lastpost", "ctime", "mtime") values
($1, '', now(), now())`
values = [page]
await db.query({
text,
values
})
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
} else {
// check rt exists
text = `select rt.* from "public"."line_fb_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
left join "public"."facebook_page" fb
on fb."id" = rt."facebook"
where
fb."id" = $1
and line."id" = $2`
values = [page, groupId]
let rt = await db.query({
text,
values
})
if (rt.rowCount === 0) {
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
}
}
// check page name in group
text = `select fb."id" from "public"."facebook_page" fb
left join "public"."line_group" line
on fb."groupid" = line."id"
where
fb."name" = $1`
values = [name]
result = await db.query({
text,
values
})
if (result.rowCount > 0) {
let reply = 'page name exists'
return { reply }
}
text = `insert into "public"."facebook_page" ("groupid", "pageid", "name", "tmpl", "ctime", "mtime") values
($1, $2, $3, $4, now(), now())`
values = [groupId, page, name, tmpl]
await db.query({
text,
values
})
let reply = 'add page success'
return { reply }
}