Skip to content

Commit

Permalink
Merge pull request #5 from talafarael/delet-_notes
Browse files Browse the repository at this point in the history
Delet  notes
  • Loading branch information
talafarael authored Apr 4, 2024
2 parents bda1d81 + adfbf49 commit 8582084
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"workbench.statusBar.visible": true
}
83 changes: 68 additions & 15 deletions src/Router/authAuction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ const User = require("../model/user")
const verifyToken = require("../middleware/verify")
import express, {Request, Response} from "express"
import {uploadFile, getFileStream} from "../s3"
const AuctionDelete = require("../model/Delet")
const checkUserOwner = require("../middleware/checkUserOwner")
const {Storage} = require("@google-cloud/storage")
const verifyTime = require("../middleware/timeMiddleware")
const projectId = "commanding-ring-409619" // Get this from Google Cloud
const keyFilename = "mykey.json"
const bcrypt = require("bcryptjs")

const generateAccessToken = require("../middleware/generateAccessToken")
const PasswordSendDelete = require("../passwordSendDelete")
// const fs = require("fs")
// const util = require("util")
// const unlinkFile = util.promisify(fs.unlink)

const passwordSendDelete = new PasswordSendDelete()
// const storage = new Storage({
// projectId,
// keyFilename,
Expand All @@ -21,29 +27,25 @@ const keyFilename = "mykey.json"
class authAuction {
async createAuction(req, res: Response) {
try {

// const file = req.file
// const result = await uploadFile(file)
if (!req.file) {
return res.status(400).send("No file uploaded.")
}


console.log(req.file)
await uploadFile(req.file);
if (!req.file) {
return res.status(400).send("No file uploaded.")
}

console.log(req.file)
await uploadFile(req.file)
// const blobStream = blob.createWriteStream();

// blobStream.on("finish", () => {
// res.status(200).send("Success");
// console.log("Success");
// });
// blobStream.end(req.file.buffer);

// const file = req.files[0]
// await unlinkFile(file.path)



const fileName = `https://faralaer.s3.eu-west-2.amazonaws.com/${req.file.originalname}`

const {title, minRates, endDate, desc, token} = req.body
Expand All @@ -61,7 +63,7 @@ class authAuction {
var currentDate = new Date()

const {user, id} = await verifyToken(token, res)
console.log(user, id)
console.log(user, id)
const auction = new Auction({
img: fileName,
title: title,
Expand Down Expand Up @@ -324,8 +326,59 @@ console.log(user, id)
res.status(400).json({message: "Registration error"})
}
}
async deleteAuctionOne(req: Request, res: Response) {
async deleteAuctionSend(req: Request, res: Response) {
try {
const {token, _id} = req.body
const {user, id} = await verifyToken(token, res)
const {checkOwner} = await checkUserOwner({res, user, _id})
const passwordUser: number = Math.floor(Math.random() * 8999) + 1000
const hashPassword = await bcrypt.hash(passwordUser.toString(), 7)
await AuctionDelete.deleteOne({idUser: id,})


await passwordSendDelete.sendmessage({
emailUser: user.email,
password: passwordUser.toString(),
})

const deleteAuction = new AuctionDelete({
idUser: id,
id: _id,
password: hashPassword,
})
deleteAuction.save()
res.status(200).json({
message: "",
})
} catch (e) {
console.log(e)
res.status(400).json({message: "Registration error"})
}
}
async deleteAuction(req: Request, res: Response) {
try {
const {token,password} = req.body
const {user, id} = await verifyToken(token, res)

const deleteAuction=await AuctionDelete.findOne({idUser: id,})
if (!deleteAuction) {
return res.status(400).json({ message: "Auction not found" });
}
console.log(deleteAuction)
const _id= deleteAuction.id
const {checkOwner} = await checkUserOwner({res, user,_id})
const validPassword = bcrypt.compareSync(password.toString(), deleteAuction.password)
if (!validPassword) {
return res
.status(400)
.json({message: `The password entered is incorrect`})
}
await Auction.deleteOne({_id: deleteAuction.id,})
await AuctionDelete.deleteOne({idUser: id,})

res.status(200).json({
message: "",
})
} catch (e) {
console.log(e)
res.status(400).json({message: "Registration error"})
Expand Down
8 changes: 1 addition & 7 deletions src/Router/authLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ const emailSender = new Emailsend()
const {Storage} = require("@google-cloud/storage")
const projectId = "commanding-ring-409619" // Get this from Google Cloud
const keyFilename = "mykey.json"
const generateAccessToken = (id) => {
const playold = {
id,
}
return jwt.sign(playold, secret, {expiresIn: "24h"})
}

const generateAccessToken=require('../middleware/generateAccessToken')
// const storage = new Storage({
// projectId,
// keyFilename,
Expand Down
3 changes: 2 additions & 1 deletion src/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ router.post('/getownauctions',middlewareUser,auctionController.getOwnAuctions)
router.post('/createauction',type,auctionController.createAuction)
router.post('/getauctioninfoforchange',auctionController.getInfoForChange)
router.post('/editfieldauction',middlewareUser,auctionController.changeInfoForChange)
router.post('/deleteauction',middlewareUser,auctionController.deleteAuction)


router.post('/deleteauctionsend',middlewareUser,auctionController.deleteAuctionSend)

module.exports = router
22 changes: 22 additions & 0 deletions src/middleware/checkUserOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { checkUserOwner } from "../type/middlewareType"


async function checkUserOwner({res,user,_id}: checkUserOwner){
try{
const Owner = await user.ownAuction
const checkOwner = Owner.find((auction: string) => auction == _id)
if (!checkOwner) {
return res.status(400).json({message: "You are not owner"})
}
console.log('aaaa')
console.log( checkOwner)
return checkOwner
} catch (error) {
return res.status(401).json({
message: "Invalid token",
});
}
}


module.exports = checkUserOwner
9 changes: 9 additions & 0 deletions src/middleware/generateAccessToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const jwt = require("jsonwebtoken")
const {secret} = require("../config")
const generateAccessToken = (id:string) => {
const playold = {
id,
}
return jwt.sign(playold, secret, {expiresIn: "24h"})
}
exports.module=generateAccessToken
8 changes: 8 additions & 0 deletions src/model/Delet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const {model, Schema} = require("mongoose")
const AuctionDelete = new Schema({
idUser:{type:String},
id:{type:String},
password:{type:String}
}, { timestamps: true })
AuctionDelete.index( { "createdAt": 1 }, { expireAfterSeconds: 420 } );
module.exports = model("AuctionDelete",AuctionDelete)
2 changes: 1 addition & 1 deletion src/model/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const user = new Schema({
balance:{type:Number},
bidAuction:{type:[]},
ownAuction:{type:[]},
img:{type:String}

})
module.exports= model("User", user)
28 changes: 28 additions & 0 deletions src/passwordSendDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require('dotenv').config()
const nodemailer=require('nodemailer')
interface SendMessageParams {
emailUser: string;
password: string;
}
class passwordSendDelete {
constructor() {
this.transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "[email protected]",
pass: "cmkvjfeqkrpqfyfz",
}
});
}


async sendmessage({emailUser, password}: SendMessageParams): Promise<void>{
await this.transporter.sendMail({
from:"[email protected]",
to:emailUser,
text:password
})
}
}

module.exports=passwordSendDelete
7 changes: 7 additions & 0 deletions src/type/middlewareType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Response} from "express"
import { IUser } from "./mongoType"
export interface checkUserOwner{
res: Response,
user:IUser,
_id:string
}
9 changes: 9 additions & 0 deletions src/type/mongoType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IUser{
img: string,
name: string,
email: string,
password: string,
balance:Number,
bidAuction:string[],
ownAuction:[],
}

0 comments on commit 8582084

Please sign in to comment.