Skip to content

Commit

Permalink
Fix + Docker based dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
corentin703 committed Feb 20, 2022
1 parent 1b38d3a commit 606eaf1
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ lib-cov

.idea
.vercel

dev/.db-migrated
dev/data/db
dev/data/pg4admin
18 changes: 9 additions & 9 deletions app/components/dashboard/articles/ExportArticles.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useQuery } from 'blitz'
import { Button } from '@mui/material'
import xlsx from 'xlsx'
import { useQuery } from 'blitz'
import Button from '@mui/material/Button'

import TableChartIcon from '@mui/icons-material/TableChart'

import getArticlesWithStats from 'app/entities/articles/queries/getArticlesWithStats'

export default function ExportArticles() {
const [{ articles }] = useQuery<any>(getArticlesWithStats, {})
const [{ articles }] = useQuery(getArticlesWithStats, {})

const exportToExcel = () => {
const date = new Date(Date.now())
const date = new Date()

const workBook = xlsx.utils.book_new()

Expand All @@ -29,10 +29,10 @@ export default function ExportArticles() {
'Prix cotisants',
'Visible',
'Ajouté le',
'Nombre vente dernière semaine',
'Nombre vente dernier mois',
'Nombre vente dernière année',
'Nombre vente total',
'Total ventes (semaine)',
'Total ventes (mois)',
'Total ventes (année)',
'Total ventes',
],
...articles.map((article) => [
article.name,
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function ExportArticles() {
}

return (
<Button endIcon={<TableChartIcon />} variant={'contained'} onClick={exportToExcel}>
<Button endIcon={<TableChartIcon />} variant="contained" onClick={exportToExcel}>
Exporter vers Excel
</Button>
)
Expand Down
8 changes: 4 additions & 4 deletions app/components/dashboard/promotions/ExportPromotions.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useQuery } from 'blitz'
import { Button } from '@mui/material'
import xlsx from 'xlsx'
import { useQuery } from 'blitz'
import Button from '@mui/material/Button'

import TableChartIcon from '@mui/icons-material/TableChart'

import getPromotions from 'app/entities/promotions/queries/getPromotions'

export default function ExportPromotions() {
const [{ promotions }] = useQuery<any>(getPromotions, {
const [{ promotions }] = useQuery(getPromotions, {
include: {
_count: {
select: {
Expand All @@ -21,7 +21,7 @@ export default function ExportPromotions() {
})

const exportToExcel = () => {
const date = new Date(Date.now())
const date = new Date()

const workBook = xlsx.utils.book_new()

Expand Down
6 changes: 3 additions & 3 deletions app/components/dashboard/users/ExportUsers.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useQuery } from 'blitz'
import { Button } from '@mui/material'
import xlsx from 'xlsx'
import { useQuery } from 'blitz'
import Button from '@mui/material/Button'

import TableChartIcon from '@mui/icons-material/TableChart'

import getUsers from 'app/entities/users/queries/getUsers'

export default function ExportUsers() {
const [{ users }] = useQuery<any>(getUsers, {
const [{ users }] = useQuery(getUsers, {
include: {
promotion: {
select: {
Expand Down
33 changes: 17 additions & 16 deletions app/components/hub/average/AverageModule.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import React, { createContext, useContext, useEffect, useState } from 'react'
import { InputLabel, MenuItem, Select } from '@mui/material'
import { createContext, useEffect, useState } from 'react'
import FormControl from '@mui/material/FormControl'
import InputLabel from '@mui/material/InputLabel'
import Typography from '@mui/material/Typography'
import MenuItem from '@mui/material/MenuItem'
import Select from '@mui/material/Select'

import { YearData, AverageStoredData } from 'constants/modules/average/types'
import AverageData from 'constants/modules/average/AverageData'
import { YearData } from 'constants/modules/average/types'
import Year from './Year'

const importData = (): { averageData: YearData[]; currentYear: number; currentSector?: number } => {
let data: any = localStorage.getItem('average_data')
if (data === null) {
data = {
averageData: AverageData,
currentYear: 0,
currentSector: undefined,
}
} else {
data = JSON.parse(data)
import Year from 'app/components/hub/average/Year'

const importData = (): AverageStoredData => {
const storedData: string | null = localStorage.getItem('average_data')

if (storedData !== null) {
return JSON.parse(storedData)
}

return data
return {
averageData: AverageData,
currentYear: 0,
currentSector: undefined,
}
}

const saveData = (averageData: YearData[], currentYear: number, currentSector?: number) => {
Expand Down
10 changes: 5 additions & 5 deletions app/components/hub/average/SectorTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useEffect, useState } from 'react'
import Paper from '@mui/material/Paper'
import Table from '@mui/material/Table'
import TableContainer from '@mui/material/TableContainer'
import Typography from '@mui/material/Typography'
import TableBody from '@mui/material/TableBody'
import TableCell from '@mui/material/TableCell'
import TableContainer from '@mui/material/TableContainer'
import TableHead from '@mui/material/TableHead'
import TableRow from '@mui/material/TableRow'
import Typography from '@mui/material/Typography'
import { useEffect, useState } from 'react'
import Paper from '@mui/material/Paper'
import Table from '@mui/material/Table'

import { SectorData, UEData } from 'constants/modules/average/types'
import UeTable from './UeTable'
Expand Down
2 changes: 1 addition & 1 deletion app/components/hub/average/Semester.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react'
import { useContext, useEffect, useState } from 'react'
import Typography from '@mui/material/Typography'

import { SectorData, SemesterData } from 'constants/modules/average/types'
Expand Down
4 changes: 2 additions & 2 deletions app/components/hub/average/Subject.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useEffect, useState } from 'react'
import { useEffect, useState } from 'react'

import { TextField } from '@mui/material'
import InputAdornment from '@mui/material/InputAdornment'
import TableRow from '@mui/material/TableRow'
import TableCell from '@mui/material/TableCell'
import { TextField } from '@mui/material'

import { SubjectData } from 'constants/modules/average/types'

Expand Down
14 changes: 7 additions & 7 deletions app/components/hub/average/UeTable.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React, { useEffect, useState } from 'react'
import Box from '@mui/material/Box'
import Collapse from '@mui/material/Collapse'
import Table from '@mui/material/Table'
import TableBody from '@mui/material/TableBody'
import TableCell from '@mui/material/TableCell'
import TableHead from '@mui/material/TableHead'
import Collapse from '@mui/material/Collapse'
import TableRow from '@mui/material/TableRow'
import { useEffect, useState } from 'react'
import Table from '@mui/material/Table'
import Box from '@mui/material/Box'

import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'
import IconButton from '@mui/material/IconButton'

import Subject from './Subject'
import { SubjectData, UEData } from 'constants/modules/average/types'
import Subject from './Subject'

interface UeTablePropsType {
ueData: UEData
Expand All @@ -23,7 +23,7 @@ const UeTable = ({ ueData, setUeData }: UeTablePropsType) => {
const [ueState, setUeState] = useState(ueData)
const [averageState, setAverageState] = useState(ueState.average)

const [open, setOpen] = React.useState(false)
const [open, setOpen] = useState(false)

useEffect(() => {
setUeData({
Expand Down
2 changes: 1 addition & 1 deletion app/components/hub/average/Year.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import { useEffect, useState } from 'react'

import { YearData } from 'constants/modules/average/types'
import Semester from './Semester'
Expand Down
40 changes: 24 additions & 16 deletions app/entities/articles/queries/getArticlesWithStats.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { resolver } from 'blitz'

import db, { Prisma } from 'db'

type GetArticlesInput = Pick<
Prisma.ArticleFindManyArgs,
'include' | 'where' | 'orderBy' | 'skip' | 'take'
>
interface ArticleWithStatsPayload extends Prisma.ArticleGetPayload<{}> {
weekCount: number,
monthCount: number,
yearCount: number,
totalCount: number,
}

export type ArticleWithStatsOutputType = {
articles: ArticleWithStatsPayload[],
nextPage: number | null,
hasMore: boolean,
count: number,
}

export default resolver.pipe(resolver.authorize(['*', 'bde']), async () => {
const todayDate = new Date(Date.now())
export default resolver.pipe(resolver.authorize(['*', 'bde']), async (): Promise<ArticleWithStatsOutputType> => {
const todayDate = new Date()
const lastWeekDate = new Date(
todayDate.getFullYear(),
todayDate.getMonth(),
Expand All @@ -25,16 +33,16 @@ export default resolver.pipe(resolver.authorize(['*', 'bde']), async () => {
todayDate.getDate()
)

const articles = await db.$queryRaw<any>`
const articles = await db.$queryRaw<ArticleWithStatsPayload[]>`
SELECT
"Article"."id",
"Article"."name",
"Article"."price",
"Article"."member_price",
"Article"."image",
"Article"."is_enabled",
"Article"."createdAt",
"Article"."updatedAt",
"Article"."id" AS "id",
"Article"."name" AS "name",
"Article"."price" AS "price",
"Article"."member_price" AS "member_price",
"Article"."image" AS "image",
"Article"."is_enabled" AS "is_enabled",
"Article"."createdAt" AS "createdAt",
"Article"."updatedAt" AS "updatedAt",
week.count AS "weekCount",
month.count AS "monthCount",
year.count AS "yearCount",
Expand Down
2 changes: 1 addition & 1 deletion constants/modules/average/ZZ1Data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SubjectData, UEData, SectorData, SemesterData, YearData } from './types'
import { SemesterData, YearData } from './types'

const ZZ1_Semester1: SemesterData = {
sectors: [
Expand Down
2 changes: 1 addition & 1 deletion constants/modules/average/ZZ2Data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SubjectData, UEData, SectorData, SemesterData, YearData } from './types'
import { SemesterData, YearData } from './types'

const ZZ2_Semester1: SemesterData = {
sectors: [
Expand Down
2 changes: 1 addition & 1 deletion constants/modules/average/ZZ3Data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SubjectData, UEData, SectorData, SemesterData, YearData } from './types'
import { SemesterData, YearData } from './types'

const ZZ3_Semester1: SemesterData = {
sectors: [
Expand Down
8 changes: 7 additions & 1 deletion constants/modules/average/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
interface AverageStoredData {
averageData: YearData[],
currentYear: number,
currentSector?: number
}

interface SubjectData {
name: string
coef: number
Expand Down Expand Up @@ -32,4 +38,4 @@ interface YearData {
average?: number
}

export type { SubjectData, UEData, SectorData, SemesterData, YearData }
export type { AverageStoredData, SubjectData, UEData, SectorData, SemesterData, YearData }
1 change: 1 addition & 0 deletions db/seeds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ main()
.finally(async () => {
await db.$disconnect()
})

6 changes: 6 additions & 0 deletions dev/clean-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

db_migrated_file="dev/.db-migrated"

rm -R dev/data/{db,pg4admin}
rm $db_migrated_file
Empty file added dev/data/.gitkeep
Empty file.
52 changes: 52 additions & 0 deletions dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: "3.7"

services:
bde_app:
container_name: bde_app
image: node:14
restart: always
working_dir: /usr/app
volumes:
- ..:/usr/app
networks:
- bde-isima-dev
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://bde_isima:isima@bde_db:5432/bde_db
entrypoint: sh -c
command: ./dev/start-dev.sh

bde_db:
container_name: bde_db
restart: always
image: postgres:latest
environment:
- POSTGRES_PASSWORD=isima
- POSTGRES_USER=bde_isima
- POSTGRES_DB=bde_db
- PGDATA=/var/lib/postgresql/data
volumes:
- ./data/db:/var/lib/postgresql/data
networks:
- bde-isima-dev
ports:
- "5432:5432"

# bde_pg4admin:
# container_name: bde_pg4admin
# restart: always
# image: dpage/pgadmin4:latest
# environment:
# - [email protected]
# - PGADMIN_DEFAULT_PASSWORD=admin
# volumes:
# - ./data/pg4admin:/var/lib/pgadmin
# ports:
# - 8080:80
# networks:
# - bde-isima-dev

networks:
bde-isima-dev:
name: bde-isima-dev
18 changes: 18 additions & 0 deletions dev/start-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

db_migrated_file="dev/.db-migrated"

set -e

npm i

if [ ! -f $db_migrated_file ]
then
npx blitz prisma migrate dev -n initial
npx prisma db seed ./db/seeds/index.ts
date > $db_migrated_file
else
echo "No migration needed"
fi

npm run dev
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ services:

networks:
bde-isima:
external: true
external: true

0 comments on commit 606eaf1

Please sign in to comment.