Skip to content

Commit

Permalink
Merge pull request #47 from Nutlope/add-locations
Browse files Browse the repository at this point in the history
Add countries to DB
  • Loading branch information
Nutlope authored Mar 25, 2023
2 parents f559f2d + 015b718 commit 567ba23
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts
.env

# Scripts
getUsersEmails.ts
18 changes: 18 additions & 0 deletions pages/api/remaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
import { authOptions } from "./auth/[...nextauth]";
import prisma from "../../lib/prismadb";
import requestIp from "request-ip";

export default async function handler(
req: NextApiRequest,
Expand All @@ -21,8 +22,25 @@ export default async function handler(
},
select: {
credits: true,
location: true,
},
});

if (!user?.location) {
const ip = requestIp.getClientIp(req);
const location = await fetch(
`http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK_API_KEY}`
).then((res) => res.json());

await prisma.user.update({
where: {
email: session.user.email!,
},
data: {
location: location.country_code,
},
});
}

return res.status(200).json({ remainingGenerations: user?.credits });
}
9 changes: 9 additions & 0 deletions prisma/migrations/20230325035657_location/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- You are about to drop the column `boughtCredits` on the `User` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "User" DROP COLUMN "boughtCredits",
ADD COLUMN "location" TEXT;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ model User {
emailVerified DateTime?
image String?
credits Int @default(3)
boughtCredits Int @default(0) // remove boughtCredits
location String?
accounts Account[]
sessions Session[]
rooms Room[]
Expand Down

1 comment on commit 567ba23

@vercel
Copy link

@vercel vercel bot commented on 567ba23 Mar 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.