messages, answered requests

- Created view for journal query so we can use the calculated fields in
a where clause
- Changed journal ordering from newest-first to oldest-first; that way,
clicking "prayed" at the top, one right after the other, will let the
user pray through their list with minimal distraction
- Answered requests now drop off the active journal (still need to write
the ability to review answered requests)
- All the activities now send messages to the user, so they have
confirmation that their action has completed
This commit is contained in:
Daniel J. Summers
2017-09-23 21:28:43 -05:00
parent 8cb58cc2d3
commit ec08bfbc74
7 changed files with 69 additions and 35 deletions

View File

@@ -52,6 +52,33 @@ const ddl = [
fix: `
CREATE INDEX "idx_request_userId" ON mpj.request ("userId");
COMMENT ON INDEX "idx_request_userId" IS 'Requests are retrieved by user'`
},
{
name: 'journal View',
check: `SELECT 1 FROM pg_views WHERE schemaname='mpj' AND viewname='journal'`,
fix: `
CREATE VIEW mpj.journal AS
SELECT
request."requestId",
request."userId",
(SELECT "text"
FROM mpj.history
WHERE history."requestId" = request."requestId"
AND "text" IS NOT NULL
ORDER BY "asOf" DESC
LIMIT 1) AS "text",
(SELECT "asOf"
FROM mpj.history
WHERE history."requestId" = request."requestId"
ORDER BY "asOf" DESC
LIMIT 1) AS "asOf",
(SELECT "status"
FROM mpj.history
WHERE history."requestId" = request."requestId"
ORDER BY "asOf" DESC
LIMIT 1) AS "lastStatus"
FROM mpj.request;
COMMENT ON VIEW mpj.journal IS 'Requests with latest text'`
}
]

View File

@@ -4,24 +4,12 @@ import { Pool } from 'pg'
import cuid from 'cuid'
const currentRequestSql = `
SELECT
request."requestId",
(SELECT "text"
FROM mpj.history
WHERE history."requestId" = request."requestId"
AND "text" IS NOT NULL
ORDER BY "asOf" DESC
LIMIT 1) AS "text",
(SELECT "asOf"
FROM mpj.history
WHERE history."requestId" = request."requestId"
ORDER BY "asOf" DESC
LIMIT 1) AS "asOf"
FROM mpj.request`
SELECT "requestId", "text", "asOf", "lastStatus"
FROM mpj.journal`
const journalSql = `${currentRequestSql}
WHERE "userId" = $1
GROUP BY request."requestId"`
AND "lastStatus" <> 'Answered'`
const requestNotFound = {
requestId: '',
@@ -40,7 +28,10 @@ export default function (pool) {
addHistory: async (requestId, status, updateText) => {
const asOf = Date.now()
await pool.query(`
INSERT INTO mpj.history ("requestId", "asOf", "status", "text") VALUES ($1, $2, $3, NULLIF($4, ''))`,
INSERT INTO mpj.history
("requestId", "asOf", "status", "text")
VALUES
($1, $2, $3, NULLIF($4, ''))`,
[ requestId, asOf, status, updateText ])
},
@@ -70,10 +61,10 @@ export default function (pool) {
} finally {
client.release()
}
return { requestId: id, text: requestText, asOf: enteredOn }
return { requestId: id, text: requestText, asOf: enteredOn, lastStatus: 'Created' }
})().catch(e => {
console.error(e.stack)
return { requestId: '', text: 'error', asOf: 0 }
return { requestId: '', text: 'error', asOf: 0, lastStatus: 'Errored' }
})
},
@@ -86,8 +77,7 @@ export default function (pool) {
byId: async (userId, requestId) => {
const reqs = await pool.query(`${currentRequestSql}
WHERE "requestId" = $1
AND "userId" = $2
GROUP BY request."requestId"`,
AND "userId" = $2`,
[ requestId, userId ])
return (0 < reqs.rowCount) ? reqs.rows[0] : requestNotFound
},
@@ -124,15 +114,7 @@ export default function (pool) {
* @param {string} userId The Id of the user
* @return The requests that make up the current journal
*/
journal: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" DESC`, [ userId ])).rows,
/**
* Get the least-recently-updated prayer request for the given user
* @param {string} userId The Id of the current user
* @return The least-recently-updated request for the given user
*/
oldest: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" LIMIT 1`, [ userId ])).rows[0]
journal: async userId => (await pool.query(`${journalSql} ORDER BY "asOf"`, [ userId ])).rows
}
}