From 7df375e7520fc2a8df00d7c3bf45ce3f3a5e08fe Mon Sep 17 00:00:00 2001 From: keyan Date: Tue, 29 Nov 2022 11:28:57 -0600 Subject: [PATCH] improvements to jobs --- api/resolvers/item.js | 3 -- api/resolvers/notifications.js | 2 +- api/resolvers/user.js | 2 +- components/item-job.js | 24 ++++++----- components/job-form.js | 7 +++- .../20221128224540_job_cost/migration.sql | 42 +++++++++++++++++++ 6 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 prisma/migrations/20221128224540_job_cost/migration.sql diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 6cc30a61..ccb3fa9f 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -236,9 +236,6 @@ export default { switch (subFull?.rankingType) { case 'AUCTION': - // it might be sufficient to sort by the floor(maxBid / 1000) desc, created_at desc - // we pull from their wallet - // TODO: need to filter out by payment status items = await models.$queryRaw(` SELECT * FROM ( diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index 06b93be6..afe97dbe 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -98,7 +98,7 @@ export default { FROM "Item" WHERE "Item"."userId" = $1 AND "maxBid" IS NOT NULL - AND "statusUpdatedAt" <= $2 + AND "statusUpdatedAt" <= $2 AND "statusUpdatedAt" <> created_at ORDER BY "sortTime" DESC LIMIT ${LIMIT}+$3)` ) diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 4ea16225..6cd59e9c 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -212,7 +212,7 @@ export default { } } }) - if (job) { + if (job && job.statusUpdatedAt > job.createdAt) { return true } diff --git a/components/item-job.js b/components/item-job.js index 7061f5a1..8c18773e 100644 --- a/components/item-job.js +++ b/components/item-job.js @@ -1,6 +1,6 @@ import * as Yup from 'yup' import Toc from './table-of-contents' -import { Button, Image } from 'react-bootstrap' +import { Badge, Button, Image } from 'react-bootstrap' import { SearchTitle } from './item' import styles from './item.module.css' import Link from 'next/link' @@ -59,16 +59,18 @@ export default function ItemJob ({ item, toc, rank, children }) { {item.mine && - <> - - \ - - - edit - - - {item.status !== 'ACTIVE' && {item.status}} - } + ( + <> + + \ + + + edit + + + {item.status !== 'ACTIVE' && {item.status}} + )} + {item.maxBid > 0 && item.status === 'ACTIVE' && PROMOTED} {toc && } diff --git a/components/job-form.js b/components/job-form.js index 11ed57d0..9d07dcbf 100644 --- a/components/job-form.js +++ b/components/job-form.js @@ -14,6 +14,7 @@ import Avatar from './avatar' import BootstrapForm from 'react-bootstrap/Form' import Alert from 'react-bootstrap/Alert' import { useMe } from './me' +import ActionTooltip from './action-tooltip' Yup.addMethod(Yup.string, 'or', function (schemas, msg) { return this.test({ @@ -183,7 +184,11 @@ export default function JobForm ({ item, sub }) { /> {item && } - {item ? 'save' : 'post'} +
+ + {item ? 'save' : <>post 1000 sats} + +
) diff --git a/prisma/migrations/20221128224540_job_cost/migration.sql b/prisma/migrations/20221128224540_job_cost/migration.sql new file mode 100644 index 00000000..36215aec --- /dev/null +++ b/prisma/migrations/20221128224540_job_cost/migration.sql @@ -0,0 +1,42 @@ +CREATE OR REPLACE FUNCTION create_job( + title TEXT, url TEXT, text TEXT, user_id INTEGER, job_bid INTEGER, job_company TEXT, + job_location TEXT, job_remote BOOLEAN, job_upload_id INTEGER) +RETURNS "Item" +LANGUAGE plpgsql +AS $$ +DECLARE + item "Item"; + user_msats BIGINT; + cost_msats BIGINT; +BEGIN + PERFORM ASSERT_SERIALIZED(); + + -- 1000 sats to create a job + cost_msats := 1000000; + + SELECT msats + INTO user_msats + FROM users + WHERE id = user_id; + + IF cost_msats > user_msats THEN + RAISE EXCEPTION 'SN_INSUFFICIENT_FUNDS'; + END IF; + + -- create item + INSERT INTO "Item" (title, url, text, "userId", "maxBid", company, location, remote, "uploadId", "subName", "statusUpdatedAt", created_at, updated_at) + VALUES (title, url, text, user_id, job_bid, job_company, job_location, job_remote, job_upload_id, 'jobs', now_utc(), now_utc(), now_utc()) RETURNING * INTO item; + + -- deduct from user + UPDATE users SET msats = msats - cost_msats WHERE id = user_id; + + -- record fee + INSERT INTO "ItemAct" (msats, "itemId", "userId", act, created_at, updated_at) + VALUES (cost_msats, item.id, user_id, 'FEE', now_utc(), now_utc()); + + -- run_auction + EXECUTE run_auction(item.id); + + RETURN item; +END; +$$; \ No newline at end of file