Skip to content

Commit

Permalink
feat: add seed to create admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuschaves committed May 3, 2023
1 parent 591b534 commit 58ea8c2
Show file tree
Hide file tree
Showing 5 changed files with 3,799 additions and 3,591 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
NODE_ENV=dev
JWT_SECRET=secretexample
JWT_SECRET=gym
PORT=3000

# DATABASE_URL="postgresql://docker:docker@localhost:5432/apisolid?schema=public"

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"npm-run-all": "4.1.5",
"prisma": "4.13.0",
"supertest": "6.3.3",
"ts-node": "10.9.1",
"tsup": "6.7.0",
"tsx": "3.12.6",
"typescript": "5.0.4",
Expand All @@ -49,5 +50,8 @@
"dotenv": "16.0.3",
"fastify": "4.16.3",
"zod": "3.21.4"
},
"prisma": {
"seed": "ts-node --transpile-only prisma/seeds/seed.ts"
}
}
24 changes: 24 additions & 0 deletions prisma/seeds/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

async function main() {
await prisma.user.upsert({
update: {},
where: { email: '[email protected]' },
create: {
email: '[email protected]',
name: 'Admin',
role: 'ADMIN',
password_hash: '$2a$06$4BUXJVVSmW5.Hspsg0qdJehnGj8sXjtypqpatJlL8s2P9fSvSuxgC', // 123456 using secret 'gym'
},
})
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
25 changes: 18 additions & 7 deletions src/http/controllers/checkIns/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { z } from "zod";
import { FastifyReply, FastifyRequest } from "fastify";
import { makeCheckInUseCase } from "@/use-cases/factories/make-check-in-use-case";
import { MaxDistanceError } from "@/use-cases/erros/max-distance-error";
import { ResourceNotFoundError } from "@/use-cases/erros/resource-not-found-error";
import { AlreadyCheckedInToday } from "@/use-cases/erros/already-checked-in-today";

export async function create(request: FastifyRequest, reply: FastifyReply) {
const createCheckInParamsSchema = z.object({
Expand All @@ -20,12 +23,20 @@ export async function create(request: FastifyRequest, reply: FastifyReply) {
const { gymId } = createCheckInParamsSchema.parse(request.params);

const createCheckInUseCase = makeCheckInUseCase();
await createCheckInUseCase.execute({
gymId,
userId: request.user.sub,
userLatitude: latitude,
userLongitude: longitude,
});

return reply.status(201).send();
try {
await createCheckInUseCase.execute({
gymId,
userId: request.user.sub,
userLatitude: latitude,
userLongitude: longitude,
});

return reply.status(201).send();
} catch (error) {
if (error instanceof MaxDistanceError || error instanceof ResourceNotFoundError || error instanceof AlreadyCheckedInToday) {
return reply.status(400).send({ message: error.message });
}
}

}
Loading

0 comments on commit 58ea8c2

Please sign in to comment.