This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolveInput: async ({ resolvedData, context }) => { | |
if (resolvedData?.products.connect.length) { | |
const products = await context.query.CartProduct.findMany({ | |
where: { | |
id: { | |
in: resolvedData?.products.connect.map( | |
(el: { id: string }) => el.id | |
), | |
}, | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validateInput: async ({ resolvedData, context, addValidationError }) => { | |
const id = resolvedData.product.connect.id; | |
const productStocks = await context.query.Product.findOne({ | |
where: { id }, | |
query: 'stock { stock amountInNextDelivery }', | |
}); | |
if ( | |
resolvedData.amount > | |
productStocks.stock.stock + productStocks.stock.amountInNextDelivery | |
) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeOperation: async ({ resolvedData, item, context }) => { | |
if (resolvedData?.products) { | |
const dbItems = await context.query.Cart.findOne({ | |
where: { id: item ? (item.id as string) : '' }, | |
query: 'products { id }', | |
}); | |
if (dbItems.products.length) { | |
await context.query.CartProduct.deleteMany({ | |
where: dbItems.products.map((el: { id: string }) => ({ | |
id: el.id, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutation { | |
updateCart( | |
where: { id: "cl2dfwo3q00210vk0s3jq6qxm" } | |
data: { | |
products: { | |
create: [ | |
{ | |
product: { connect: { id: "cl2dfz56505040vk0ggih6mll" } } | |
amount: 5 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { list } from '@keystone-6/core'; | |
import { integer, relationship } from '@keystone-6/core/fields'; | |
export const CartProduct = list({ | |
fields: { | |
product: relationship({ ref: 'Product' }), | |
amount: integer({ defaultValue: 0, validation: { isRequired: true } }), | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import data from './data.json'; | |
import { PrismaClient } from '@prisma/client'; | |
const prisma = new PrismaClient(); | |
async function main() { | |
const { users } = data; | |
for await (const user of users) { | |
const currentUser = await prisma.user.findFirst({ | |
where: { email: user.email }, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"users": [ | |
{ | |
"name": "Admin", | |
"email": "admin@gmail.com", | |
"password": "$2a$10$mJBzYnc/23jilS8.V1AKgOPlwOtjOcF.wAEoz29qk8SxYno/o6aYC", | |
"role": "admin" | |
}, | |
{ | |
"name": "Employee", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { list } from '@keystone-6/core'; | |
import { text, password, select, relationship } from '@keystone-6/core/fields'; | |
import { RolesValues } from '../consts/roles.const'; | |
import { Roles } from '../enums/roles.enum'; | |
import { filterCustomerAccess, filterCustomerAccessCreate } from '../shared'; | |
export const User = list({ | |
fields: { | |
name: text({ validation: { isRequired: true } }), | |
email: text({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
access: { | |
operation: { | |
create: ({ session }) => | |
!!session && session.data.role !== Roles.Customer, | |
update: ({ session }) => | |
!!session && session.data.role !== Roles.Customer, | |
delete: ({ session }) => | |
!!session && session.data.role !== Roles.Customer, | |
}, | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { list } from '@keystone-6/core'; | |
import { decimal, relationship, timestamp } from '@keystone-6/core/fields'; | |
import { filterCustomerAccess } from '../shared'; | |
export const Cart = list({ | |
fields: { | |
user: relationship({ | |
ref: 'User', | |
ui: { | |
hideCreate: true, |
NewerOlder