Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main ts #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion backend/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
env: {
browser: true,
es2021: true
},
extends: 'standard',
overrides: [
],
parserOptions: {
Expand Down
File renamed without changes.
16 changes: 5 additions & 11 deletions backend/middlewares/auth.js → backend/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import jwt from 'jsonwebtoken'
import { JWT_SECRET } from '../consts/secret.js'
/**
* Middleware function type import
* @typedef {import('express').RequestHandler} RequestHandler
*/
import { JWT_SECRET } from '../consts/secret'
import {Request, Response, NextFunction} from "express";
import {Token} from "../types/types";

/**
* Middleware for authentification
* @type {RequestHandler}
*/
const authMiddleware = (req, res, next) => {
const authMiddleware = (req: Request & {user?: Token}, res: Response, next: NextFunction) => {
// Récupération du token d'authentification depuis le header Authorization
const authHeader = req.headers.authorization
const token = authHeader && authHeader.split(' ')[1]
Expand All @@ -26,7 +20,7 @@ const authMiddleware = (req, res, next) => {
}

// Ajout des informations du token décodé à l'objet de requête pour être utilisées par les routes suivantes
req.user = decodedToken
req.user = decodedToken as Token
next()
})
}
Expand Down
3 changes: 0 additions & 3 deletions backend/orm/database.js

This file was deleted.

11 changes: 11 additions & 0 deletions backend/orm/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Sequelize } from 'sequelize-typescript';
import {Product} from "./models/product.js";
import {Bid} from "./models/bid.js";
import {User} from "./models/user.js";

export const sequelize = new Sequelize({
database: 'db',
dialect: 'sqlite',
storage: ':memory:',
models: [Bid, Product, User],
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Bid, Product, User } from '../index.js'

export async function regenerateFixtures () {
await Bid.destroy({ where: { } })
await User.destroy({ where: { } })
await Product.destroy({ where: { } })
await User.destroy({ where: { } })

const DAY = 24 * 60 * 60 * 1000

Expand Down Expand Up @@ -224,7 +224,7 @@ export async function regenerateFixtures () {
if (Math.random() < 0.8) {
let price = product.originalPrice

let lastBuyer = null
let lastBuyer: null | undefined | User = null

for (let date = new Date(Date.now() + DAY); date < product.endDate; date = new Date(date.getTime() + DAY / 4)) {
if (Math.random() < 0.2) {
Expand All @@ -236,7 +236,7 @@ export async function regenerateFixtures () {

await Bid.create({
productId: product.id,
bidderId: bidder.id,
bidderId: bidder?.id,
price,
date
})
Expand Down
19 changes: 0 additions & 19 deletions backend/orm/index.js

This file was deleted.

10 changes: 10 additions & 0 deletions backend/orm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {User} from './models/user.js'
import {Bid} from './models/bid.js'
import {Product} from './models/product.js'
import { sequelize } from './database.js'

export async function initializeDatabase () {
return await sequelize.sync()
}

export { User, Bid, Product }
43 changes: 0 additions & 43 deletions backend/orm/models/bid.js

This file was deleted.

53 changes: 53 additions & 0 deletions backend/orm/models/bid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { DataTypes } from 'sequelize'
import {Table, Column, Model, BelongsTo} from 'sequelize-typescript';
import {Product} from "./product.js";
import {User} from "./user.js";

@Table
export class Bid extends Model {

@Column({
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
})
declare id: string

@Column({
type: DataTypes.UUID,
allowNull: false
})
declare productId: string

@Column({
type: DataTypes.UUID,
allowNull: false
})
declare bidderId: string

@Column({
type: DataTypes.INTEGER,
allowNull: false,
validate: {
isInt: true,
min: 0
}
})
declare price: number

@Column({
type: DataTypes.DATE,
allowNull: false,
validate: {
isDate: true
}
})
declare date: Date

@BelongsTo(() => Product, {foreignKey: 'productId'})
declare product: Awaited<Product>;

@BelongsTo(() => User, {foreignKey: 'bidderId'})
declare bidder: Awaited<User>;
}
73 changes: 43 additions & 30 deletions backend/orm/models/product.js → backend/orm/models/product.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,83 @@
import { DataTypes } from 'sequelize'
import { sequelize } from '../database.js'
import {Table, Column, Model, HasMany, BelongsTo} from 'sequelize-typescript';
import {User} from "./user.js";
import {Bid} from "./bid.js";

/**
* @typedef {Object} ProductObject
* @property {string} id
* @property {string} name
* @property {string} description
* @property {string} category
* @property {number} originalPrice
* @property {string} pictureUrl
* @property {Date} endDate
* @property {number} sellerId
*/
@Table
export class Product extends Model {

export default sequelize.define('Product', {
id: {
@Column({
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
name: {
})
declare id: string

@Column({
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
description: {
})
declare name: string

@Column({
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
category: {
})
declare description: string

@Column({
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
originalPrice: {
})
declare category: string

@Column({
type: DataTypes.INTEGER,
allowNull: false,
validate: {
isInt: true,
min: 0
}
},
pictureUrl: {
})
declare originalPrice: number

@Column({
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
endDate: {
})
declare pictureUrl: string

@Column({
type: DataTypes.DATE,
allowNull: false,
validate: {
isDate: true
}
},
sellerId: {
})
declare endDate: Date

@Column({
type: DataTypes.UUID,
allowNull: false
}
})
})
declare sellerId: string

@BelongsTo(() => User, {foreignKey: 'sellerId'})
declare seller: Awaited<User>;

@HasMany(() => Bid, {foreignKey: 'productId', onDelete: 'CASCADE'})
declare bids: Awaited<Bid[]>;
}
50 changes: 0 additions & 50 deletions backend/orm/models/user.js

This file was deleted.

Loading