diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 8a8cd9de..db796a0d 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -49,46 +49,62 @@ export default { connectAddress: async (parent, args, { lnd }) => { return process.env.LND_CONNECT_ADDRESS }, - walletHistory: async (parent, { cursor }, { me, models, lnd }) => { + walletHistory: async (parent, { cursor, inc }, { me, models, lnd }) => { const decodedCursor = decodeCursor(cursor) if (!me) { throw new AuthenticationError('you must be logged in') } + const include = new Set(inc.split(',')) + const queries = [] + + if (include.has('invoice')) { + queries.push( + `(SELECT ('invoice' || id) as id, id as "factId", bolt11, created_at as "createdAt", + COALESCE("msatsReceived", "msatsRequested") as msats, NULL as "msatsFee", + CASE WHEN "confirmedAt" IS NOT NULL THEN 'CONFIRMED' + WHEN "expiresAt" <= $2 THEN 'EXPIRED' + WHEN cancelled THEN 'CANCELLED' + ELSE 'PENDING' END as status, + 'invoice' as type + FROM "Invoice" + WHERE "userId" = $1 + AND created_at <= $2)`) + } + + if (include.has('withdrawal')) { + queries.push( + `(SELECT ('withdrawal' || id) as id, id as "factId", bolt11, created_at as "createdAt", + CASE WHEN status = 'CONFIRMED' THEN "msatsPaid" + ELSE "msatsPaying" END as msats, + CASE WHEN status = 'CONFIRMED' THEN "msatsFeePaid" + ELSE "msatsFeePaying" END as "msatsFee", + COALESCE(status::text, 'PENDING') as status, + 'withdrawal' as type + FROM "Withdrawl" + WHERE "userId" = $1 + AND created_at <= $2)`) + } + // TODO // 1. union invoices and withdrawals (check) // 2. add to union spending and receiving + if (queries.length === 0) { + return { + cursor: null, + facts: [] + } + } + let history = await models.$queryRaw(` - (SELECT id, bolt11, created_at as "createdAt", - COALESCE("msatsReceived", "msatsRequested") as msats, NULL as "msatsFee", - CASE WHEN "confirmedAt" IS NOT NULL THEN 'CONFIRMED' - WHEN "expiresAt" <= $2 THEN 'EXPIRED' - WHEN cancelled THEN 'CANCELLED' - ELSE 'PENDING' END as status, - 'invoice' as type - FROM "Invoice" - WHERE "userId" = $1 - AND created_at <= $2 - ORDER BY created_at desc - LIMIT ${LIMIT}+$3) - UNION ALL - (SELECT id, bolt11, created_at as "createdAt", - CASE WHEN status = 'CONFIRMED' THEN "msatsPaid" - ELSE "msatsPaying" END as msats, - CASE WHEN status = 'CONFIRMED' THEN "msatsFeePaid" - ELSE "msatsFeePaying" END as "msatsFee", - COALESCE(status::text, 'PENDING') as status, - 'withdrawal' as type - FROM "Withdrawl" - WHERE "userId" = $1 - AND created_at <= $2 - ORDER BY created_at desc - LIMIT ${LIMIT}+$3) + ${queries.join(' UNION ALL ')} ORDER BY "createdAt" DESC OFFSET $3 LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) + console.log(history) + history = history.map(f => { if (f.bolt11) { const inv = lnpr.decode(f.bolt11) @@ -113,6 +129,8 @@ export default { return f }) + console.log(history) + return { cursor: history.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, facts: history diff --git a/api/ssrApollo.js b/api/ssrApollo.js index 07177eff..6817bd7a 100644 --- a/api/ssrApollo.js +++ b/api/ssrApollo.js @@ -30,7 +30,7 @@ export default async function getSSRApolloClient (req, me = null) { } export function getGetServerSideProps (query, variables = null, foundField) { - return async function ({ req, params }) { + return async function ({ req, query: params }) { const client = await getSSRApolloClient(req) const { error, data } = await client.query({ query, diff --git a/api/typeDefs/wallet.js b/api/typeDefs/wallet.js index ae444ab6..b623636f 100644 --- a/api/typeDefs/wallet.js +++ b/api/typeDefs/wallet.js @@ -5,7 +5,7 @@ export default gql` invoice(id: ID!): Invoice! withdrawl(id: ID!): Withdrawl! connectAddress: String! - walletHistory(cursor: String): History + walletHistory(cursor: String, inc: String): History } extend type Mutation { @@ -41,6 +41,7 @@ export default gql` type Fact { id: ID! + factId: ID! bolt11: String createdAt: String! msats: Int! diff --git a/components/form.js b/components/form.js index 9c13c658..905d15f5 100644 --- a/components/form.js +++ b/components/form.js @@ -175,33 +175,31 @@ export function Input ({ label, groupClassName, ...props }) { ) } -export function Checkbox ({ children, label, extra, handleChange, ...props }) { +export function Checkbox ({ children, label, extra, handleChange, inline, ...props }) { // React treats radios and checkbox inputs differently other input types, select, and textarea. // Formik does this too! When you specify `type` to useField(), it will // return the correct bag of props for you - const [field, { value }] = useField({ ...props, type: 'checkbox' }) + const [field] = useField({ ...props, type: 'checkbox' }) return ( -
type | -detail | -sats | -
---|---|---|
{f.type} | -
-
- {f.description || 'no description'}
-
- |
- {f.msats / 1000} | -
type | +detail | +sats | +
---|---|---|
{f.type} | +
+
+ {f.description || 'no description'}
+
+ |
+ {f.msats / 1000} | +