diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 5c21de90..d712c634 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -532,7 +532,7 @@ export default { const checkSats = async () => { // check if the user has the funds to run for the first minute - const minuteMsats = maxBid * 1000 / 30 / 24 / 60 + const minuteMsats = maxBid / 1296 const user = models.user.findUnique({ where: { id: me.id } }) if (user.msats < minuteMsats) { throw new UserInputError('insufficient funds') diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 3253f09d..6251ac2c 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -159,7 +159,7 @@ export default { if (me?.id !== user.id) { return 0 } - return Math.floor(user.msats / 1000) + return Math.floor(user.msats / 1000.0) }, bio: async (user, args, { models }) => { return getItem(user, { id: user.bioId }, { models }) diff --git a/api/typeDefs/user.js b/api/typeDefs/user.js index 41b83899..0c2ec1ea 100644 --- a/api/typeDefs/user.js +++ b/api/typeDefs/user.js @@ -46,7 +46,6 @@ export default gql` tipDefault: Int! bio: Item sats: Int! - msats: Int! upvotePopover: Boolean! tipPopover: Boolean! } diff --git a/prisma/migrations/20220226164520_status_update/migration.sql b/prisma/migrations/20220226164520_status_update/migration.sql new file mode 100644 index 00000000..55caefc5 --- /dev/null +++ b/prisma/migrations/20220226164520_status_update/migration.sql @@ -0,0 +1,33 @@ +-- 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; \ No newline at end of file diff --git a/prisma/migrations/20220226170533_msats_bigint/migration.sql b/prisma/migrations/20220226170533_msats_bigint/migration.sql new file mode 100644 index 00000000..0644a838 --- /dev/null +++ b/prisma/migrations/20220226170533_msats_bigint/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "users" ALTER COLUMN "msats" SET DEFAULT 0, +ALTER COLUMN "msats" SET DATA TYPE BIGINT; diff --git a/prisma/migrations/20220226174624_int/migration.sql b/prisma/migrations/20220226174624_int/migration.sql new file mode 100644 index 00000000..f5bdad1f --- /dev/null +++ b/prisma/migrations/20220226174624_int/migration.sql @@ -0,0 +1,41 @@ +/* + 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; \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1e9201b4..122ff980 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -109,15 +109,15 @@ model Item { subName String? @db.Citext // fields exclusively for job post types right now - minSalary Int? - maxSalary Int? - maxBid Int? - status Status @default(ACTIVE) - noSatsAt DateTime? - location String? - latitude Float? - longitude Float? - remote Boolean? + minSalary Int? + maxSalary Int? + maxBid Int? + status Status @default(ACTIVE) + statusUpdatedAt DateTime? + location String? + latitude Float? + longitude Float? + remote Boolean? User User[] @relation("Item")