Skip to content

Commit

Permalink
[AI] Prevent AI usage from production (#26)
Browse files Browse the repository at this point in the history
* [Resolver] Add mandala chart related resolver (#21)

* ✨ Add mandala model

* ♻️ Change way to export

* ✨ Add default value

* ✨ Add mandala chart resolver

* ✨ Save title and description on mandala chart document, not cell

* ✨ Add dto for creating mandala chart

* ✨ Add dto for updating mandala chart

* ✨ Add dto for deleting mandala chart

* 🔥 Remove message field from success type

* ✨ Add dto for Getting mandala chart

* ✨ Add resolver for mandala chart

* ♻️ Fix type error

* ✨ Change goals to goal per one mandala cell

* ✨ Add getting user id decorator for resolver

* ✨ Add return type of failure

* ✨ Get userId from cookie on saving

* ✨ Add private field and fix wrong type

* ✨ Imbed mandala cell into mandala chart

* ✨ Add private fields on input

* ✨ Change update input

* 🐛 Remove true in for loop on validation function

* ✨ Replace impossible decorator to middleware

* 🔧 Fix eslintrc error

* ✨ Set temporary ignore

* 🐛 Fix error on type graphql

MichalLytek/type-graphql#1401

* ♻️ Change error without login

* ✨ Apply userId middleware

* ✨ Set proper error type

* [Graphql] Apply changed type of mandala chart on web (#22)

* 🔥 Remove grid

* ✨ Set mock data for dev

* ✨ Show placeholder

* ✨ Add isGridValid

* 🚚 Move screenshot button

* ✨ Add load and save button

* ✨ Add buttons

* ✨ Apply i18n

* 🚚 Move mandala chart section to reuse

* 💄 Change gap between grid

* 💄 Change text style of the center square

* 💄 Add style on displaying mandala chart

* ✨ Seperate default value

* 🚚 Move ai recommendation query hook

* 🚚 Seperate switch

* ✨ Change update mandala chart input type

* ♻️ Add more var

* 🚧 Change whole grid value type

* 🚧 Change displaying full view mandala chart type with graphql type

* 🚧 Change use ai recommendation type with graphql type

* ✨ Changed type for mandala chart

* 🐛 Fix handle manually function

* 🐛 Fix handle ai function

* 🐛 Fix set ai mode

* 💄 Set opacity

* 🐛 Fix wrong dev value

* 🔥 Remove test value

* 🔥 Remove dev value

* 🐛 Fix opacity

* ✨ Add keyboard event

* [Web] Add mandala chart list page (#23)

* ✨ Add getUserMandalaCharts query

* ✨ Send filtered mandala charts

* ✨ Make mandala chart description nullable

* ✨ Get mandala charts

* ✨ Add variables to query

🔥 Remove unnecessary console

* 💄 Add icon

* 💄 Add lock icon

* ✨ Add created at and modified at date

* ✨ Make last modified at non essential

* ✨ Add created updated information

* ✨ Set sign in

✨ Connect to sign in page when click loading saved charts button

* ✨ Apply delete mutation

* ✨ Add no content

* 💄 Change style

* ✨ Set link to mandala chart page

* [Save] Add save and load feature (#24)

* 🚚 Copy index page to chart page

* 🔥 Remove no mandala charts

* ♻️ Change props

* ✨ Add query on the chart page

* ✨ Skip if no chart id exist

* 🔥 Remove unnecessary var

* ✨ Add error type

* ✨ Add Title and Description component

* ✨ Add title and description edit on chart

* ✨ Update response type of mandala chart

* 🔥 Remove unnecessary handler function and vars

* ✨ Make create button to save in server

* 🔥 Remove duplicated variables input

* ✨ Add alert for network work

* 🐛 Remove as option

* ✨ Change input type of update

* ♻️ Apply changed chart id

* 🔥 Remove typename to prevent error

* ✨ Change phrase

* ✨ Add private button

* ✨ Prevent request if there is no title

* 🔥 Remove dev default value

* ✨ Make use go to can keep the params

* ✨ Add temp save

* 🐛 Fix wrong condition

* ✨ Update chart will show alert

* [i18n] Apply i18n (#25)

* ✨ Change home page to explain about website and mandala chart

* 💄 Change header style

* ✨ Control font size on mobile

* 🐛 Control font size on mobile

* ✨ Add sidebar

* ✨ Remove menus from mobile and move to sign out

* ✨ Add icons

* ✨ Make sidebar cover the page

* ✨ Add sign in out on nav bar

* 🚚 Move layout realted file into a file

* :turck: Change file name

* ✨ Apply i18n on layout

* 🚚 Move values to common i18n

* ✨ Apply i18n on my list

* 💄 Fix style

* 💄 Change displaying mandala chart style

* ✨ Add pupetter for screenshot on server

* ✨ Get screenshot on the client

* ✨ Add styles on public

* ✨ Change scripts

* ✨ Get screenshot from server

* ✨ Get proper size image

* ✨ Add example charts

* 🔥 Show placeholder on display too

* ✨ Add empty example

* 💄 Change align

* ✨ Apply handle function

* ✨ Do not show sign up or sign in if it's already signed in

* ✨ Add i18n on index page

* 💄 Change style

* ♻️ Change i18n

* ✨ Change build command

* ♻️ Change i18n

* ✨ Add number

* ✨ Add notice

* ✨ Comment AI mode switch

* ✨ Do not open when grid is not valid

* ✨ Add focus to prevent escaping

* ✨ Add border for focused square

* ✨ Prevent ai recommend

* ✨ Add colors on highlight box
  • Loading branch information
lcpnine authored Jan 28, 2024
1 parent 4327740 commit 3bd6048
Show file tree
Hide file tree
Showing 73 changed files with 5,305 additions and 589 deletions.
21 changes: 13 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"plugins": ["unused-imports"],
"extends":["@typescript-eslint/no-unused-vars"],
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
{ "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" }
]
}
}
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
{
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^_"
}
]
}
}
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"nodemailer": "^6.9.8",
"openai": "^4.24.7",
"passport-local": "^1.0.0",
"puppeteer": "^21.9.0",
"reflect-metadata": "^0.2.1",
"serialport": "^12.0.0",
"type-graphql": "^1.1.1",
Expand Down
26 changes: 25 additions & 1 deletion packages/server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import express from 'express'
import { createYoga } from 'graphql-yoga'
import mongoose from 'mongoose'
import createYogaConfig from './config/yoga'
import { IS_DEV, PHASE } from './constant/common'
import { CLIENT_URL, IS_DEV, PHASE } from './constant/common'
import { captureScreenshot } from './utils/screenshot'

const startApp = async () => {
console.log('PHASE: ', PHASE)
Expand All @@ -38,12 +39,35 @@ const startApp = async () => {
})
)

const res = await fetch(`${CLIENT_URL}/styles.css`, {
method: 'GET',
})
const styleSheet = await res.text()

mongoose.connect(process.env.MONGO_URI as string)

const yogaConfig = await createYogaConfig()
const yoga = createYoga(yogaConfig)
app.use('/graphql', yoga)

// for screenshot
app.post('/screenshot', async (req, res) => {
try {
const htmlContent = req.body.html
const { width, height } = req.body
const screenshotBuffer = await captureScreenshot(htmlContent, {
styleSheet,
width,
height,
})
res.writeHead(200, { 'Content-Type': 'image/png' })
res.end(screenshotBuffer, 'binary')
} catch (error) {
console.error('Error capturing screenshot:', error)
res.status(500).send('Internal Server Error')
}
})

const PORT = process.env.PORT || 4003

app.listen(PORT, () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/config/yoga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import type { YogaServerOptions } from 'graphql-yoga'
import { buildSchema } from 'type-graphql'
import { IS_DEV } from '../constant/common'
import renewJwt from '../middlewares/renewJwt.middleware'
import { extractUserId } from '../middlewares/userId.middleware'
import { MandalaChartResolver } from '../resolvers/mandalaChart.resolver'
import { RecommendationResolver } from '../resolvers/recommendation.resolver'
import { UserResolver } from '../resolvers/user.resolver'
import { MyContext } from '../types/common'

const createSchema = async () =>
await buildSchema({
resolvers: [UserResolver, RecommendationResolver],
resolvers: [UserResolver, MandalaChartResolver, RecommendationResolver],
emitSchemaFile: 'src/schema.gql',
validate: {
forbidUnknownValues: false,
},
})

const createYogaConfig = async () => {
Expand All @@ -20,6 +25,7 @@ const createYogaConfig = async () => {
graphiql: IS_DEV,
context: async ({ req, res }: MyContext) => {
await renewJwt(req, res)
extractUserId(req, res)
return { req, res }
},
} as YogaServerOptions<{}, {}>
Expand Down
10 changes: 0 additions & 10 deletions packages/server/src/controllers/healthCheck.ts

This file was deleted.

29 changes: 29 additions & 0 deletions packages/server/src/middlewares/userId.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Request, Response } from 'express'
import * as jwt from 'jsonwebtoken'

export const extractUserId = (req: Request, res: Response) => {
const token = req.cookies?.token

if (!token) {
req.userId = null
return
}

try {
const decoded = jwt.verify(
token,
process.env.JWT_SECRET as string
) as jwt.JwtPayload
req.userId = decoded.userId
} catch (error) {
req.userId = null
}
}

declare global {
namespace Express {
interface Request {
userId: string | null
}
}
}
39 changes: 39 additions & 0 deletions packages/server/src/models/MandalaChart.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import mongoose, { Document, Schema } from 'mongoose'

// MandalaCell schema remains the same
const MandalaCellSchema: Schema = new Schema({
goal: { type: String },
tasks: [{ type: String }],
})

export interface IMandalaCell extends Document {
goal: string
tasks: string[]
}

const MandalaChartSchema: Schema = new Schema({
title: { type: String, required: true },
description: { type: String },
userId: { type: Schema.Types.ObjectId, ref: 'User' },
private: { type: Boolean, default: false },
centerCell: MandalaCellSchema,
createdAt: { type: Date, default: Date.now },
lastModifiedAt: { type: Date },
surroundingCells: [MandalaCellSchema],
})

export interface IMandalaChart extends Document {
title: string
description?: string
userId: mongoose.Types.ObjectId
private: boolean
centerCell: IMandalaCell
createdAt: Date
lastModifiedAt?: Date
surroundingCells: IMandalaCell[]
}

export const MandalaChartModel = mongoose.model<IMandalaChart>(
'MandalaChart',
MandalaChartSchema
)
7 changes: 6 additions & 1 deletion packages/server/src/models/User.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const UserSchema: Schema = new Schema({
},
default: () => ({}),
},
mandalaCharts: {
type: [{ type: Schema.Types.ObjectId, ref: 'MandalaChart' }],
default: [],
},
})

export interface IUser extends Document {
Expand All @@ -32,6 +36,7 @@ export interface IUser extends Document {
emailVerification: TokenInfo
resetPassword: TokenInfo
purchasedInfo: PurchasedInfo
mandalaCharts: mongoose.Types.ObjectId[]
}

export default mongoose.model<IUser>('User', UserSchema)
export const UserModel = mongoose.model<IUser>('User', UserSchema)
Loading

0 comments on commit 3bd6048

Please sign in to comment.