Skip to content

Commit 64d1c45

Browse files
committed
fix: inftrastructure improvements
1 parent d4bdecf commit 64d1c45

File tree

14 files changed

+57
-128
lines changed

14 files changed

+57
-128
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ GOOGLE_CLIENT_SECRET=
1111
GOOGLE_REDIRECT_URI=
1212
PORT=
1313
API_PREFIX=
14-
NODE_ENV=
1514
ALLOWED_ORIGINS=
1615
ACCESS_JWT_SECRET=
1716
REFRESH_JWT_SECRET=

app/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const app = express()
1111

1212
app.use(helmet())
1313
app.use(cors({ origin: env.ALLOWED_ORIGINS }))
14-
app.use(logger(env.NODE_ENV === 'development' ? 'dev' : 'combined'))
14+
app.use(logger(app.get('env') === 'development' ? 'dev' : 'combined'))
1515
app.use(express.json())
1616

1717
app.use(env.API_PREFIX, apiRouter)

app/config/env.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const envSchema = z.object({
2626
z.coerce.number().int().positive()
2727
),
2828
API_PREFIX: z.string(),
29-
NODE_ENV: z.enum(['development', 'production']),
3029
ALLOWED_ORIGINS: z
3130
.string()
3231
.transform(v => v.split(','))

app/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { transport } from './mailer.config'
22
export { default } from './cloudinary.config'
33
export { env } from './env.config'
4-
export { redis } from './redis.config'
4+
export { redisClient } from './redis.config'

app/config/redis.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Redis from 'ioredis'
22

33
import { env } from './env.config'
44

5-
export const redis = new Redis({
5+
export const redisClient = new Redis({
66
username: env.REDIS_USERNAME,
77
password: env.REDIS_PASSWORD,
88
host: env.REDIS_HOST,

app/controllers/auth.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Conflict, Forbidden, Unauthorized } from 'http-errors'
1616
import { jwtVerify, SignJWT } from 'jose'
1717
import { JWTExpired } from 'jose/errors'
1818

19-
import { env, redis } from '@/config'
19+
import { env, redisClient } from '@/config'
2020

2121
const {
2222
ACCESS_JWT_EXPIRES_IN,
@@ -96,7 +96,7 @@ class AuthController {
9696
getGoogleRedirectUrl = async (_: Request, res: Response) => {
9797
const state = crypto.randomBytes(32).toString('hex')
9898

99-
await redis.set(`oauth_state:${state}`, 'true', 'EX', 5 * 60)
99+
await redisClient.set(`oauth_state:${state}`, 'true', 'EX', 5 * 60)
100100

101101
const url = this.googleClient.generateAuthUrl({
102102
state,
@@ -116,10 +116,10 @@ class AuthController {
116116

117117
const redisStateKey = `oauth_state:${receivedState}`
118118

119-
const storedState = await redis.get(redisStateKey)
119+
const storedState = await redisClient.get(redisStateKey)
120120

121121
if (storedState) {
122-
await redis.del(redisStateKey)
122+
await redisClient.del(redisStateKey)
123123
}
124124

125125
const { tokens } = await this.googleClient.getToken(code)

app/controllers/board.controller.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ import type {
1414
import { prisma } from '@/prisma'
1515
import { NotFound } from 'http-errors'
1616

17-
import { redis } from '@/config'
17+
import { redisClient } from '@/config'
1818
import boardImages from '@/data/board-bg-images.json'
1919

2020
class BoardController {
2121
getAll = async ({ user }: Request, res: Response) => {
2222
const cacheKey = `boards:user:${user.id}:all`
2323

24-
const cachedBoards = await redis.get(cacheKey)
24+
const cachedBoards = await redisClient.get(cacheKey)
2525

2626
if (cachedBoards) {
2727
res.json(JSON.parse(cachedBoards))
2828
} else {
2929
const boards = await prisma.board.findMany({ where: { userId: user.id } })
3030

31-
await redis.set(cacheKey, JSON.stringify(boards))
31+
await redisClient.set(cacheKey, JSON.stringify(boards))
3232

3333
res.json(boards)
3434
}
@@ -41,7 +41,7 @@ class BoardController {
4141
) => {
4242
const cacheKey = `board:${params.boardId}:user:${user.id}`
4343

44-
const cachedBoard = await redis.get(cacheKey)
44+
const cachedBoard = await redisClient.get(cacheKey)
4545

4646
if (cachedBoard) {
4747
res.json(JSON.parse(cachedBoard))
@@ -58,7 +58,7 @@ class BoardController {
5858

5959
if (!board) return next(NotFound('Board not found'))
6060

61-
await redis.set(cacheKey, JSON.stringify(board))
61+
await redisClient.set(cacheKey, JSON.stringify(board))
6262

6363
res.json(board)
6464
}
@@ -76,7 +76,7 @@ class BoardController {
7676
}
7777
})
7878

79-
await redis.del(`boards:user:${user.id}:all`)
79+
await redisClient.del(`boards:user:${user.id}:all`)
8080

8181
res.json(newBoard)
8282
}
@@ -100,8 +100,8 @@ class BoardController {
100100

101101
if (!updatedBoard) return next(NotFound('Board not found'))
102102

103-
await redis.del(`board:${updatedBoard.id}:user:${user.id}`)
104-
await redis.del(`boards:user:${user.id}:all`)
103+
await redisClient.del(`board:${updatedBoard.id}:user:${user.id}`)
104+
await redisClient.del(`boards:user:${user.id}:all`)
105105

106106
res.json(updatedBoard)
107107
}
@@ -117,8 +117,8 @@ class BoardController {
117117

118118
if (!deletedBoard) return next(NotFound('Board not found'))
119119

120-
await redis.del(`board:${deletedBoard.id}:user:${user.id}`)
121-
await redis.del(`boards:user:${user.id}:all`)
120+
await redisClient.del(`board:${deletedBoard.id}:user:${user.id}`)
121+
await redisClient.del(`boards:user:${user.id}:all`)
122122

123123
res.sendStatus(204)
124124
}

app/controllers/card.controller.ts

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
CardParamsSchema,
44
ColumnParamsSchema,
55
EditCardSchema,
6-
MoveCardSchema,
76
UpdateCardOrderSchema
87
} from '@/schemas'
98
import type { NextFunction, Response } from 'express'
@@ -13,7 +12,7 @@ import type { TypedRequest, TypedRequestParams } from 'zod-express-middleware'
1312
import { prisma } from '@/prisma'
1413
import { BadRequest, NotFound } from 'http-errors'
1514

16-
import { redis } from '@/config'
15+
import { redisClient } from '@/config'
1716

1817
class CardController {
1918
add = async (
@@ -26,7 +25,7 @@ class CardController {
2625
) => {
2726
const column = await prisma.column.findFirst({
2827
where: { id: params.columnId },
29-
select: { id: true, boardId: true, board: { select: { userId: true } } }
28+
include: { board: { select: { userId: true } } }
3029
})
3130

3231
if (!column) return next(NotFound('Column not found'))
@@ -37,7 +36,7 @@ class CardController {
3736
data: { ...body, columnId: column.id, order: newOrder }
3837
})
3938

40-
await redis.del(`board:${column.boardId}:user:${column.board.userId}`)
39+
await redisClient.del(`board:${column.boardId}:user:${column.board.userId}`)
4140

4241
res.json(newCard)
4342
}
@@ -50,23 +49,28 @@ class CardController {
5049
res: Response,
5150
next: NextFunction
5251
) => {
52+
if (body.columnId) {
53+
const column = await prisma.column.findFirst({
54+
where: { id: body.columnId },
55+
include: { board: { select: { userId: true } } }
56+
})
57+
58+
if (!column) return next(NotFound('Column not found'))
59+
}
60+
5361
const updatedCard = await prisma.card.updateIgnoreNotFound({
5462
where: { id: params.cardId },
5563
data: body,
56-
select: {
57-
column: {
58-
select: { boardId: true, board: { select: { userId: true } } }
59-
}
60-
}
64+
include: { column: { include: { board: { select: { userId: true } } } } }
6165
})
6266

6367
if (!updatedCard) return next(NotFound('Card not found'))
6468

65-
await redis.del(
66-
`board:${updatedCard.column.boardId}:user:${updatedCard.column.board.userId}`
67-
)
69+
const { column, ...card } = updatedCard
70+
71+
await redisClient.del(`board:${column.boardId}:user:${column.board.userId}`)
6872

69-
res.json(updatedCard)
73+
res.json(card)
7074
}
7175

7276
updateOrder = async (
@@ -83,7 +87,7 @@ class CardController {
8387
) => {
8488
const column = await prisma.column.findFirst({
8589
where: { id: params.columnId },
86-
select: { id: true, boardId: true, board: { select: { userId: true } } }
90+
include: { board: { select: { userId: true } } }
8791
})
8892

8993
if (!column) return next(NotFound('Column not found'))
@@ -98,57 +102,29 @@ class CardController {
98102
try {
99103
const updatedCards = await prisma.$transaction(transaction)
100104

101-
await redis.del(`board:${column.boardId}:user:${column.board.userId}`)
105+
await redisClient.del(
106+
`board:${column.boardId}:user:${column.board.userId}`
107+
)
102108

103109
res.json(updatedCards)
104110
} catch {
105111
return next(BadRequest('Invalid order'))
106112
}
107113
}
108114

109-
move = async (
110-
{ params }: TypedRequestParams<typeof MoveCardSchema>,
111-
res: Response,
112-
next: NextFunction
113-
) => {
114-
const column = await prisma.column.findFirst({
115-
where: { id: params.newColumnId },
116-
select: { id: true, boardId: true, board: { select: { userId: true } } }
117-
})
118-
119-
if (!column) return next(NotFound('Column not found'))
120-
121-
const newOrder = await this.getNewCardOrder(column.id)
122-
123-
const updatedCard = await prisma.card.updateIgnoreNotFound({
124-
where: { id: params.cardId },
125-
data: { columnId: column.id, order: newOrder }
126-
})
127-
128-
if (!updatedCard) return next(NotFound('Card not found'))
129-
130-
await redis.del(`board:${column.boardId}:user:${column.board.userId}`)
131-
132-
res.json(updatedCard)
133-
}
134-
135115
deleteById = async (
136116
{ params }: TypedRequestParams<typeof CardParamsSchema>,
137117
res: Response,
138118
next: NextFunction
139119
) => {
140120
const deletedCard = await prisma.card.deleteIgnoreNotFound({
141121
where: { id: params.cardId },
142-
select: {
143-
column: {
144-
select: { boardId: true, board: { select: { userId: true } } }
145-
}
146-
}
122+
include: { column: { include: { board: { select: { userId: true } } } } }
147123
})
148124

149125
if (!deletedCard) return next(NotFound('Card not found'))
150126

151-
await redis.del(
127+
await redisClient.del(
152128
`board:${deletedCard.column.boardId}:user:${deletedCard.column.board.userId}`
153129
)
154130

app/controllers/column.controller.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { TypedRequest, TypedRequestParams } from 'zod-express-middleware'
1212
import { prisma } from '@/prisma'
1313
import { BadRequest, NotFound } from 'http-errors'
1414

15-
import { redis } from '@/config'
15+
import { redisClient } from '@/config'
1616

1717
class ColumnController {
1818
add = async (
@@ -42,7 +42,7 @@ class ColumnController {
4242
data: { ...body, order: newOrder, boardId: board.id }
4343
})
4444

45-
await redis.del(`board:${params.boardId}:user:${user.id}`)
45+
await redisClient.del(`board:${params.boardId}:user:${user.id}`)
4646

4747
res.json(column)
4848
}
@@ -61,17 +61,17 @@ class ColumnController {
6161
) => {
6262
const updatedColumn = await prisma.column.updateIgnoreNotFound({
6363
where: { id: params.columnId },
64-
select: { boardId: true, board: { select: { userId: true } } },
64+
include: { cards: true, board: { select: { userId: true } } },
6565
data: body
6666
})
6767

6868
if (!updatedColumn) return next(NotFound('Column not found'))
6969

70-
await redis.del(
71-
`board:${updatedColumn.boardId}:user:${updatedColumn.board.userId}`
72-
)
70+
const { board, ...column } = updatedColumn
71+
72+
await redisClient.del(`board:${updatedColumn.boardId}:user:${board.userId}`)
7373

74-
res.json(updatedColumn)
74+
res.json(column)
7575
}
7676

7777
updateOrder = async (
@@ -102,7 +102,7 @@ class ColumnController {
102102
try {
103103
const updatedColumns = await prisma.$transaction(transaction)
104104

105-
await redis.del(`board:${board.id}:user:${board.userId}`)
105+
await redisClient.del(`board:${board.id}:user:${board.userId}`)
106106

107107
res.json(updatedColumns)
108108
} catch {
@@ -122,7 +122,7 @@ class ColumnController {
122122

123123
if (!deletedColumn) return next(NotFound('Column not found'))
124124

125-
await redis.del(
125+
await redisClient.del(
126126
`board:${deletedColumn.boardId}:user:${deletedColumn.board.userId}`
127127
)
128128

app/controllers/user.controller.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import { prisma } from '@/prisma'
88
import { hash } from 'argon2'
99
import { Conflict, InternalServerError, NotAcceptable } from 'http-errors'
1010

11-
import { env, redis } from '@/config'
12-
import cloudinary from '@/config/cloudinary.config'
13-
import { transport } from '@/config/mailer.config'
11+
import cloudinary, { env, redisClient, transport } from '@/config'
1412

1513
class UserController {
1614
me = async (req: Request, res: Response) => {
1715
const cacheKey = `user:${req.user.id}`
1816

19-
const cachedUser = await redis.get(cacheKey)
17+
const cachedUser = await redisClient.get(cacheKey)
2018

2119
if (cachedUser) {
2220
res.json(JSON.parse(cachedUser))
@@ -25,7 +23,7 @@ class UserController {
2523
where: { id: req.user.id }
2624
})
2725

28-
await redis.set(cacheKey, JSON.stringify(user))
26+
await redisClient.set(cacheKey, JSON.stringify(user))
2927

3028
res.json(user)
3129
}
@@ -84,7 +82,7 @@ class UserController {
8482
data: updateData
8583
})
8684

87-
await redis.del(`user:${updatedUser.id}`)
85+
await redisClient.del(`user:${updatedUser.id}`)
8886

8987
res.json(updatedUser)
9088
}

0 commit comments

Comments
 (0)