From 31be14e8b4adae270f761a870a5f26b8124a1806 Mon Sep 17 00:00:00 2001 From: Kurt Hutten Date: Mon, 9 Nov 2020 20:43:59 +1100 Subject: [PATCH] Add seed data to seed.js good for getting others up to speed --- README.md | 9 +++++ api/prisma/seeds.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ api/src/lib/auth.js | 4 ++ 3 files changed, 105 insertions(+) diff --git a/README.md b/README.md index 6970be4..8061bdc 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ yarn install Initialise the db ``` terminal yarn rw db up +yarn rw db seed ``` ### Fire up dev @@ -36,6 +37,14 @@ yarn rw dev Your browser should open automatically to `http://localhost:8910` to see the web app. Lambda functions run on `http://localhost:8911` and are also proxied to `http://localhost:8910/.redwood/functions/*`. +you can sign in to the following accounts locally + +localUser1@kurthutten.com: `abc123` + +localUser2@kurthutten.com: `abc123` + +localAdmin@kurthutten.com: `abc123` + You may need to register a account depending on what issue you are trying to tackle, This can be done by clicking the login button on the top right. This will open up netlify's idenitiy modal asking for the websites url, since it will notice you developing locally. Enter `https://cadhub.xyz/` than use you email, verify your email and you should be set. (some routes are protected, but permissions is a big area that needs a lot of work in the near future, so it's in a very incomplete state atm) diff --git a/api/prisma/seeds.js b/api/prisma/seeds.js index 51426c5..31d3730 100644 --- a/api/prisma/seeds.js +++ b/api/prisma/seeds.js @@ -15,6 +15,98 @@ async function main() { // if (!existing.length) { // await db.user.create({ data: { name: 'Admin', email: 'admin@email.com' }}) // } + const users = [ + { + id: "a2b21ce1-ae57-43a2-b6a3-b6e542fd9e60", + userName: "local-user-1", + name: "local 1", + email: "localUser1@kurthutten.com" + }, + { + id: "682ba807-d10e-4caf-bf28-74054e46c9ec", + userName: "local-user-2", + name: "local 2", + email: "localUser2@kurthutten.com" + }, + { + id: "5cea3906-1e8e-4673-8f0d-89e6a963c096", + userName: "local-admin-2", + name: "local admin", + email: "localAdmin@kurthutten.com" + }, + ] + + let existing + existing = await db.user.findMany({ where: { id: users[0].id }}) + if(!existing.length) { + await db.user.create({ + data: users[0], + }) + } + existing = await db.user.findMany({ where: { id: users[1].id }}) + if(!existing.length) { + await db.user.create({ + data: users[1], + }) + } + + const parts = [ + { + title: 'demo-part1', + description: '# can be markdown', + mainImage: 'CadHub/kjdlgjnu0xmwksia7xox', + user: { + connect: { + id: users[0].id, + }, + }, + }, + { + title: 'demo-part2', + description: '## [hey](www.google.com)', + user: { + connect: { + id: users[1].id, + }, + }, + }, + ] + + existing = await db.part.findMany({where: { title: parts[0].title}}) + if(!existing.length) { + await db.part.create({ + data: parts[0], + }) + } + existing = await db.part.findMany({where: { title: parts[1].title}}) + if(!existing.length) { + await db.part.create({ + data: parts[1], + }) + } + + + + const aPart = await db.part.findOne({where: { + title_userId: { + title: parts[0].title, + userId: users[0].id, + } + }}) + await db.comment.create({ + data: { + text: "nice part, I like it", + user: {connect: { id: users[0].id}}, + part: {connect: { id: aPart.id}}, + } + }) + await db.partReaction.create({ + data: { + emote: "❤️", + user: {connect: { id: users[0].id}}, + part: {connect: { id: aPart.id}}, + } + }) console.info('No data to seed. See api/prisma/seeds.js for info.') } diff --git a/api/src/lib/auth.js b/api/src/lib/auth.js index 402f218..3889bbc 100644 --- a/api/src/lib/auth.js +++ b/api/src/lib/auth.js @@ -141,4 +141,8 @@ export const requireAuth = ({ role } = {}) => { ) { throw new ForbiddenError("You don't have access to do that.") } + + if(context.currentUser?.sub === '5cea3906-1e8e-4673-8f0d-89e6a963c096') { + throw new ForbiddenError("That's a local admin ONLY.") + } }