Skip to content

Commit

Permalink
Upgrade Blitz (#28)
Browse files Browse the repository at this point in the history
* Upgrade Blitz

- Change imports and base files
- Upgrade Prisma
- Remove old migrations
- Create initial migration
- Change Prisma db types

* Ignore tests for now

* Delete backup
  • Loading branch information
Gabriel Chertok authored Mar 5, 2021
1 parent 3332922 commit cabefeb
Show file tree
Hide file tree
Showing 64 changed files with 1,433 additions and 4,833 deletions.
2 changes: 1 addition & 1 deletion app/auth/mutations/logout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ctx } from "blitz"

export default async function logout(_: any, { session }: Ctx) {
return await session.revoke()
return await session.$revoke()
}
1 change: 1 addition & 0 deletions app/channels/lib/getUserChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default async function getUserChannels({ user }: { user: User }) {
const result = (await web.users.conversations({
user: user?.slackUserId,
types: "public_channel,private_channel",
limit: 300,
})) as WebAPICallResult & { channels: Channel[] }

return result.channels
Expand Down
2 changes: 1 addition & 1 deletion app/channels/queries/getChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type GetChannelInput = { where: { id: string } }
export type Result = WebAPICallResult & { channel: Channel }

export default async function getChannel({ where }: GetChannelInput, ctx: Ctx): Promise<Result> {
ctx.session.authorize()
ctx.session.$authorize()

const user = await db.user.findFirst({ where: { id: ctx.session.userId } })

Expand Down
2 changes: 1 addition & 1 deletion app/channels/queries/getChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebClient } from "@slack/web-api"
export type GetChannelsInput = { limit?: number; cursor?: string }

export default async function getChannels({ limit = 300, cursor }: GetChannelsInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const user = await db.user.findFirst({ where: { id: ctx.session.userId } })

Expand Down
3 changes: 1 addition & 2 deletions app/guard/ability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { DeleteReactionInput } from "app/reactions/mutations/deleteReaction"
type ExtendedResourceTypes = PrismaModelsType<typeof db>

export default GuardBuilder<ExtendedResourceTypes>(async (ctx, { can, cannot }) => {
cannot("manage", "all")
if (ctx.session.isAuthorized()) {
if (ctx.session.$isAuthorized()) {
// Messages
can("read", "message", async ({ where }: GetMessageInput) => {
const messageRequest = db.message.findFirst({ where: { id: where?.id } })
Expand Down
2 changes: 1 addition & 1 deletion app/messageViews/mutations/createMessageView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Ctx } from "blitz"
import db from "db"

export default async function createMessageView({ messageId }: { messageId: string }, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const messageView = await db.messageView.upsert({
create: {
Expand Down
4 changes: 3 additions & 1 deletion app/messages/components/reactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { useMutation, useQuery, setQueryData } from "blitz"

export type ReactionsProps = {
messageId: string
userId: string
userId: string | null
}

export default function Reactions({ messageId, userId }: ReactionsProps) {
if (!userId) return null

return (
<div className="flex">
{Object.keys(emojis).map((key: EmojiKey) => {
Expand Down
2 changes: 2 additions & 0 deletions app/messages/components/slack-channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useQuery } from "blitz"
export type SlackChannelProps = JSX.IntrinsicElements["small"] & { channelId: string }
export default function SlackChannel({ className = "", channelId, ...rest }: SlackChannelProps) {
const [response] = useQuery(getChannel, { where: { id: channelId } }, { enabled: !!channelId })
if (!response) return null

return (
<small className={`text-xss ${className}`} {...rest}>
<span className="bg-gray-300 py-1 px-2">#{response.channel.name}</span>
Expand Down
2 changes: 1 addition & 1 deletion app/messages/mutations/createMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Guard from "app/guard/ability"
type CreateMessageInputType = Pick<Prisma.MessageCreateArgs, "data">

async function createMessage({ data }: CreateMessageInputType, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()
const { title, body, slackChannelId } = CreateMessageInput.parse(data)

const message = await db.message.create({
Expand Down
2 changes: 1 addition & 1 deletion app/messages/mutations/deleteMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Guard from "app/guard/ability"
export type DeleteMessageInput = Pick<Prisma.MessageDeleteArgs, "where">

async function deleteMessage({ where }: DeleteMessageInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const message = await db.message.delete({ where })

Expand Down
2 changes: 1 addition & 1 deletion app/messages/mutations/updateMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UpdateMessageInput } from "app/messages/validations"
export type UpdateMessageInputType = Pick<Prisma.MessageUpdateArgs, "data" | "where">

async function updateMessage({ where, data }: UpdateMessageInputType, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const { title, body } = UpdateMessageInput.parse(data)

Expand Down
4 changes: 2 additions & 2 deletions app/messages/queries/getMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Guard from "app/guard/ability"
import { Ctx, NotFoundError } from "blitz"
import db, { Prisma } from "db"

export type GetMessageInput = Pick<Prisma.FindFirstMessageArgs, "where" | "include">
export type GetMessageInput = Pick<Prisma.MessageFindFirstArgs, "where" | "include">

async function getMessage({ where }: GetMessageInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()
const message = await db.message.findFirst({
where,
include: { user: true, views: { include: { user: true } } },
Expand Down
10 changes: 6 additions & 4 deletions app/reactions/mutations/createReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Guard from "app/guard/ability"
export type CreateReactionInput = Pick<Prisma.ReactionCreateArgs, "data">

async function createReaction({ data }: CreateReactionInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const existingReaction = await db.reaction.findFirst({
where: {
Expand All @@ -17,17 +17,19 @@ async function createReaction({ data }: CreateReactionInput, ctx: Ctx) {
})
if (existingReaction) return existingReaction

data.user = { connect: { id: ctx.session.userId } }

const reaction = await db.reaction.create({
data: { ...data, user: { connect: { id: ctx.session.userId } } },
include: { message: { include: { user: true } } },
data,
include: { message: true, user: true },
})

if (reaction.message?.slackTimeStamp && process.env.NODE_ENV !== "test") {
await AddQueue.enqueue({
channel: reaction.message.slackChannelId,
timestamp: reaction.message.slackTimeStamp,
name: data.alt,
userToken: reaction.message.user?.slackAccessToken as string,
userToken: reaction.user?.slackAccessToken as string,
})
}

Expand Down
6 changes: 3 additions & 3 deletions app/reactions/mutations/deleteReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import Guard from "app/guard/ability"
export type DeleteReactionInput = Pick<Prisma.ReactionDeleteArgs, "where">

async function deleteReaction({ where }: DeleteReactionInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const reaction = await db.reaction.delete({
where,
include: { message: { include: { user: true } } },
include: { message: true, user: true },
})

if (reaction.message?.slackTimeStamp) {
await RemoveQueue.enqueue({
channel: reaction.message.slackChannelId,
timestamp: reaction.message.slackTimeStamp,
name: reaction.alt,
userToken: reaction.message.user?.slackAccessToken as string,
userToken: reaction.user?.slackAccessToken as string,
})
}

Expand Down
4 changes: 2 additions & 2 deletions app/reactions/queries/getReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Guard from "app/guard/ability"
import { Ctx, NotFoundError } from "blitz"
import db, { Prisma } from "db"

type GetReactionInput = Pick<Prisma.FindFirstReactionArgs, "where">
type GetReactionInput = Pick<Prisma.ReactionFindFirstArgs, "where">

async function getReaction({ where }: GetReactionInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const reaction = await db.reaction.findFirst({ where })

Expand Down
4 changes: 2 additions & 2 deletions app/reactions/queries/getReactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Guard from "app/guard/ability"
import { Ctx } from "blitz"
import db, { Prisma } from "db"

type GetReactionsInput = Pick<Prisma.FindManyReactionArgs, "where" | "orderBy" | "skip" | "take">
type GetReactionsInput = Pick<Prisma.ReactionFindManyArgs, "where" | "orderBy" | "skip" | "take">

async function getReactions({ where, orderBy, skip = 0, take }: GetReactionsInput, ctx: Ctx) {
ctx.session.authorize()
ctx.session.$authorize()

const reactions = await db.reaction.findMany({
where,
Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
presets: ["next/babel"],
presets: ["blitz/babel"],
plugins: [],
}
7 changes: 5 additions & 2 deletions blitz.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { sessionMiddleware, unstable_simpleRolesIsAuthorized } = require("@blitzjs/server")
const { sessionMiddleware, simpleRolesIsAuthorized } = require("blitz")
const { BlitzGuardMiddleware } = require("@blitz-guard/core/dist/middleware.js")

module.exports = {
Expand All @@ -12,9 +12,12 @@ module.exports = {
],
}),
sessionMiddleware({
unstable_isAuthorized: unstable_simpleRolesIsAuthorized,
isAuthorized: simpleRolesIsAuthorized,
}),
],
experimental: {
isomorphicResolverImports: false,
},
/* Uncomment this to customize the webpack config
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
// Note: we provide webpack above so you should not `require` it
Expand Down
15 changes: 3 additions & 12 deletions db/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { enhancePrisma } from "blitz"
import { PrismaClient } from "@prisma/client"
export * from "@prisma/client"

let prisma: PrismaClient

if (process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test") {
prisma = new PrismaClient()
} else {
// Ensure the prisma instance is re-used during hot-reloading
// Otherwise, a new client will be created on every reload
globalThis["prisma"] = globalThis["prisma"] || new PrismaClient()
prisma = globalThis["prisma"]
}

export default prisma
const EnhancedPrisma = enhancePrisma(PrismaClient)
export default new EnhancedPrisma()
87 changes: 0 additions & 87 deletions db/migrations/20201231214134-initial-migration/README.md

This file was deleted.

38 changes: 0 additions & 38 deletions db/migrations/20201231214134-initial-migration/schema.prisma

This file was deleted.

Loading

1 comment on commit cabefeb

@vercel
Copy link

@vercel vercel bot commented on cabefeb Mar 5, 2021

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.