remove extra migrations, refine jobs
This commit is contained in:
parent
3fb7ab9cd7
commit
022c72b95b
|
@ -78,7 +78,7 @@ export default {
|
|||
let items; let user; let pins; let subFull
|
||||
|
||||
const subClause = (num) => {
|
||||
return sub ? ` AND "subName" = $${num} ` : ` AND ("subName" IS NULL OR "subName" = $${3}) `
|
||||
return sub ? ` AND "subName" = $${num} ` : ` AND ("subName" IS NULL OR "subName" = $${num}) `
|
||||
}
|
||||
|
||||
const activeOrMine = () => {
|
||||
|
@ -165,9 +165,10 @@ export default {
|
|||
${timedLeftJoinWeightedSats(1)}
|
||||
WHERE "parentId" IS NULL AND created_at <= $1 AND created_at > $3
|
||||
AND "pinId" IS NULL
|
||||
${subClause(4)}
|
||||
${timedOrderBySats(1)}
|
||||
OFFSET $2
|
||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date() - 7))
|
||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date() - 7), sub || 'NULL')
|
||||
}
|
||||
|
||||
if (decodedCursor.offset !== 0 || items?.length < LIMIT) {
|
||||
|
@ -177,9 +178,10 @@ export default {
|
|||
${timedLeftJoinWeightedSats(1)}
|
||||
WHERE "parentId" IS NULL AND created_at <= $1
|
||||
AND "pinId" IS NULL
|
||||
${subClause(3)}
|
||||
${timedOrderBySats(1)}
|
||||
OFFSET $2
|
||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset)
|
||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, sub || 'NULL')
|
||||
}
|
||||
|
||||
if (decodedCursor.offset === 0) {
|
||||
|
@ -532,8 +534,9 @@ export default {
|
|||
|
||||
const checkSats = async () => {
|
||||
// check if the user has the funds to run for the first minute
|
||||
const minuteMsats = maxBid / 1296
|
||||
const user = models.user.findUnique({ where: { id: me.id } })
|
||||
const minuteMsats = maxBid * 5 / 216
|
||||
const user = await models.user.findUnique({ where: { id: me.id } })
|
||||
console.log(user, user.msats, minuteMsats)
|
||||
if (user.msats < minuteMsats) {
|
||||
throw new UserInputError('insufficient funds')
|
||||
}
|
||||
|
|
|
@ -104,6 +104,14 @@ export default {
|
|||
WHERE "Invite"."userId" = $1
|
||||
AND users.created_at <= $2
|
||||
GROUP BY "Invite".id)
|
||||
UNION ALL
|
||||
(SELECT "Item".id::text, "Item"."statusUpdatedAt" AS "sortTime", NULL as "earnedSats",
|
||||
'JobChanged' AS type
|
||||
FROM "Item"
|
||||
WHERE "Item"."userId" = $1
|
||||
AND "maxBid" IS NOT NULL
|
||||
AND status <> 'STOPPED'
|
||||
AND "statusUpdatedAt" <= $2)
|
||||
ORDER BY "sortTime" DESC
|
||||
OFFSET $3
|
||||
LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset)
|
||||
|
@ -129,6 +137,9 @@ export default {
|
|||
Reply: {
|
||||
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
||||
},
|
||||
JobChanged: {
|
||||
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
||||
},
|
||||
Mention: {
|
||||
mention: async (n, args, { models }) => true,
|
||||
item: async (n, args, { models }) => getItem(n, { id: n.id }, { models })
|
||||
|
|
|
@ -210,6 +210,23 @@ export default {
|
|||
return true
|
||||
}
|
||||
|
||||
const job = await models.item.findFirst({
|
||||
where: {
|
||||
status: {
|
||||
not: 'STOPPED'
|
||||
},
|
||||
maxBid: {
|
||||
not: null
|
||||
},
|
||||
statusUpdatedAt: {
|
||||
gt: user.checkedNotesAt
|
||||
}
|
||||
}
|
||||
})
|
||||
if (job) {
|
||||
return true
|
||||
}
|
||||
|
||||
// check if new invites have been redeemed
|
||||
const newInvitees = await models.$queryRaw(`
|
||||
SELECT "Invite".id
|
||||
|
|
|
@ -27,7 +27,12 @@ export default gql`
|
|||
sortTime: String!
|
||||
}
|
||||
|
||||
union Notification = Reply | Votification | Mention | Invitification
|
||||
type JobChanged {
|
||||
item: Item!
|
||||
sortTime: String!
|
||||
}
|
||||
|
||||
union Notification = Reply | Votification | Mention | Invitification | JobChanged
|
||||
|
||||
type Notifications {
|
||||
lastChecked: String
|
||||
|
|
|
@ -92,7 +92,7 @@ export default function JobForm ({ item, sub }) {
|
|||
<div>
|
||||
<ol>
|
||||
<li>You only pay as many sats/mo as required to maintain your position relative to other
|
||||
posts and only up to your max bid.
|
||||
posts (or {sub.baseCost} sats/mo) and only up to your max bid.
|
||||
</li>
|
||||
<li>Your sats/mo must be a multiple of 1000 sats</li>
|
||||
</ol>
|
||||
|
@ -147,7 +147,7 @@ export default function JobForm ({ item, sub }) {
|
|||
if (item) {
|
||||
router.push(`/items/${item.id}`)
|
||||
} else {
|
||||
router.push(`/$${sub.name}/recent`)
|
||||
router.push(`/~${sub.name}/recent`)
|
||||
}
|
||||
})}
|
||||
>
|
||||
|
@ -198,9 +198,11 @@ export default function JobForm ({ item, sub }) {
|
|||
function StatusControl ({ item }) {
|
||||
let StatusComp
|
||||
|
||||
if (item.status === 'ACTIVE') {
|
||||
if (item.status !== 'STOPPED') {
|
||||
StatusComp = () => {
|
||||
return (
|
||||
<>
|
||||
|
||||
<AccordianItem
|
||||
header={<div style={{ fontWeight: 'bold', fontSize: '92%' }}>I want to stop my job</div>}
|
||||
headerColor='var(--danger)'
|
||||
|
@ -210,9 +212,10 @@ function StatusControl ({ item }) {
|
|||
/>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
} else if (item.status === 'STOPPED') {
|
||||
} else {
|
||||
StatusComp = () => {
|
||||
return (
|
||||
<AccordianItem
|
||||
|
@ -226,18 +229,14 @@ function StatusControl ({ item }) {
|
|||
/>
|
||||
)
|
||||
}
|
||||
} else {
|
||||
StatusComp = () => {
|
||||
return (
|
||||
<div style={{ fontWeight: 'bold', color: 'var(--danger)' }}>
|
||||
you have no sats! <Link href='/wallet?type=fund' passHref><a className='text-reset text-underline'>fund your wallet</a></Link> to resume your job
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='my-2'>
|
||||
{item.status === 'NOSATS' &&
|
||||
<div className='text-danger font-weight-bold my-1'>
|
||||
you have no sats! <Link href='/wallet?type=fund' passHref><a className='text-reset text-underline'>fund your wallet</a></Link> to resume your job
|
||||
</div>}
|
||||
<StatusComp />
|
||||
</div>
|
||||
)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { useQuery } from '@apollo/client'
|
||||
import Comment, { CommentSkeleton } from './comment'
|
||||
import Item from './item'
|
||||
import Item, { ItemJob } from './item'
|
||||
import { NOTIFICATIONS } from '../fragments/notifications'
|
||||
import { useRouter } from 'next/router'
|
||||
import MoreFooter from './more-footer'
|
||||
|
@ -58,8 +58,16 @@ function Notification ({ n }) {
|
|||
<small className='font-weight-bold text-info ml-2'>
|
||||
you were mentioned in
|
||||
</small>}
|
||||
<div className={n.__typename === 'Votification' || n.__typename === 'Mention' ? '' : 'py-2'}>
|
||||
{n.item.title
|
||||
{n.__typename === 'JobChanged' &&
|
||||
<small className={`font-weight-bold text-${n.item.status === 'NOSATS' ? 'danger' : 'success'} ml-1`}>
|
||||
{n.item.status === 'NOSATS'
|
||||
? 'your job ran out of sats'
|
||||
: 'your job is active again'}
|
||||
</small>}
|
||||
<div className={n.__typename === 'Votification' || n.__typename === 'Mention' || n.__typename === 'JobChanged' ? '' : 'py-2'}>
|
||||
{n.item.maxBid
|
||||
? <ItemJob item={n.item} />
|
||||
: n.item.title
|
||||
? <Item item={n.item} />
|
||||
: (
|
||||
<div className='pb-2'>
|
||||
|
|
|
@ -41,6 +41,12 @@ export const NOTIFICATIONS = gql`
|
|||
...InviteFields
|
||||
}
|
||||
}
|
||||
... on JobChanged {
|
||||
sortTime
|
||||
item {
|
||||
...ItemFields
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} `
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
ALTER TABLE "Item" RENAME COLUMN "noSatsAt" TO "statusUpdatedAt";
|
||||
|
||||
-- charge the user for the auction item
|
||||
CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
|
||||
DECLARE
|
||||
|
@ -9,21 +11,21 @@ CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
|
|||
PERFORM ASSERT_SERIALIZED();
|
||||
|
||||
-- extract data we need
|
||||
SELECT ("maxBid" * 1000 / 30 / 24 / 60), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
|
||||
SELECT ("maxBid" * 5 / 216), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
|
||||
SELECT msats INTO user_msats FROM users WHERE id = user_id;
|
||||
|
||||
-- check if user wallet has enough sats
|
||||
IF bid > user_msats THEN
|
||||
-- if not, set status = NOSATS and noSatsAt to now_utc if not already set
|
||||
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
|
||||
IF item_status <> 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'NOSATS', "noSatsAt" = now_utc() WHERE id = item_id;
|
||||
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
ELSE
|
||||
-- if so, deduct from user
|
||||
UPDATE users SET msats = msats - bid WHERE id = user_id;
|
||||
-- update item status = ACTIVE and noSatsAt = null if NOSATS
|
||||
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
|
||||
IF item_status = 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'ACTIVE', "noSatsAt" = null WHERE id = item_id;
|
||||
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "Item" RENAME COLUMN "noSatsAt" TO "statusUpdatedAt";
|
||||
|
||||
-- charge the user for the auction item
|
||||
CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
|
||||
DECLARE
|
||||
bid INTEGER;
|
||||
user_id INTEGER;
|
||||
user_msats INTEGER;
|
||||
item_status "Status";
|
||||
BEGIN
|
||||
PERFORM ASSERT_SERIALIZED();
|
||||
|
||||
-- extract data we need
|
||||
SELECT ("maxBid" * 1000 / 30 / 24 / 60), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
|
||||
SELECT msats INTO user_msats FROM users WHERE id = user_id;
|
||||
|
||||
-- check if user wallet has enough sats
|
||||
IF bid > user_msats THEN
|
||||
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
|
||||
IF item_status <> 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
ELSE
|
||||
-- if so, deduct from user
|
||||
UPDATE users SET msats = msats - bid WHERE id = user_id;
|
||||
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
|
||||
IF item_status = 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
|
@ -1,3 +0,0 @@
|
|||
-- AlterTable
|
||||
ALTER TABLE "users" ALTER COLUMN "msats" SET DEFAULT 0,
|
||||
ALTER COLUMN "msats" SET DATA TYPE BIGINT;
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to alter the column `msats` on the `users` table. The data in that column could be lost. The data in that column will be cast from `BigInt` to `Integer`.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "users" ALTER COLUMN "msats" SET DEFAULT 0,
|
||||
ALTER COLUMN "msats" SET DATA TYPE INTEGER;
|
||||
|
||||
|
||||
-- charge the user for the auction item
|
||||
CREATE OR REPLACE FUNCTION run_auction(item_id INTEGER) RETURNS void AS $$
|
||||
DECLARE
|
||||
bid INTEGER;
|
||||
user_id INTEGER;
|
||||
user_msats INTEGER;
|
||||
item_status "Status";
|
||||
BEGIN
|
||||
PERFORM ASSERT_SERIALIZED();
|
||||
|
||||
-- extract data we need
|
||||
SELECT ("maxBid" * 5 / 216), "userId", status INTO bid, user_id, item_status FROM "Item" WHERE id = item_id;
|
||||
SELECT msats INTO user_msats FROM users WHERE id = user_id;
|
||||
|
||||
-- check if user wallet has enough sats
|
||||
IF bid > user_msats THEN
|
||||
-- if not, set status = NOSATS and statusUpdatedAt to now_utc if not already set
|
||||
IF item_status <> 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'NOSATS', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
ELSE
|
||||
-- if so, deduct from user
|
||||
UPDATE users SET msats = msats - bid WHERE id = user_id;
|
||||
-- update item status = ACTIVE and statusUpdatedAt = null if NOSATS
|
||||
IF item_status = 'NOSATS' THEN
|
||||
UPDATE "Item" SET status = 'ACTIVE', "statusUpdatedAt" = now_utc() WHERE id = item_id;
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
Loading…
Reference in New Issue