parent
ee937b8c3e
commit
ca8d8a2bd0
@ -1,67 +1,55 @@
|
|||||||
import express, { Application } from 'express';
|
import express, { Application } from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
|
import { MONGO_PASSWORD } from './config';
|
||||||
import cookieParser from 'cookie-parser';
|
import cookieParser from 'cookie-parser';
|
||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import Controller from './controller/interfaces/IController';
|
import Controller from './controllers/interfaces/IController';
|
||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
public express: Application;
|
public express: Application;
|
||||||
public port: number;
|
public port: number;
|
||||||
public dataBase: null;
|
public dataBase: null;
|
||||||
|
|
||||||
public server: any;
|
public server: any;
|
||||||
|
|
||||||
constructor(controllers: Controller[], port: number) {
|
constructor(controllers: Controller[], port: number) {
|
||||||
this.express = express();
|
this.express = express();
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.dataBase = null;
|
|
||||||
|
|
||||||
this.initialiseDatabase();
|
|
||||||
this.initialiseMiddleware();
|
|
||||||
this.initialiseControllers(controllers);
|
|
||||||
|
|
||||||
|
this.initDatabase();
|
||||||
|
this.initMiddleware();
|
||||||
|
this.initControllers(controllers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private initialiseMiddleware(): void {
|
private initMiddleware(): void {
|
||||||
this.express.use(cors());
|
this.express.use(cors());
|
||||||
this.express.use(cookieParser());
|
this.express.use(cookieParser());
|
||||||
|
|
||||||
|
|
||||||
this.express.use(express.json());
|
this.express.use(express.json());
|
||||||
this.express.use(express.urlencoded({ extended: false }));
|
this.express.use(express.urlencoded({ extended: false }));
|
||||||
|
|
||||||
this.express.use(bodyParser.json());
|
this.express.use(bodyParser.json());
|
||||||
this.express.use(bodyParser.urlencoded({
|
this.express.use(bodyParser.urlencoded({
|
||||||
extended: true
|
extended: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private initialiseControllers(controllers: Controller[]): void {
|
private initControllers(controllers: Controller[]): void {
|
||||||
controllers.forEach((controller: Controller) => {
|
controllers.forEach((controller: Controller) => {
|
||||||
this.express.use('/api', controller.router);
|
this.express.use('/api', controller.router);
|
||||||
this.express.get('/toto', (req, res) => {
|
|
||||||
res.send('Hello World!');
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public listen(): void {
|
public listen(): void {
|
||||||
const server = this.express.listen(this.port, () => {
|
this.express.listen(this.port, () => {
|
||||||
console.log(`[server] : App listening on the port ${this.port}`);
|
console.log(`[server] : App listening on the port ${this.port}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private initialiseDatabase(): void {
|
private initDatabase(): void {
|
||||||
const MONGO_URL = "mongodb+srv://fladDevDb:ZslYlNRWIOUU7i6o@fladcluster.b29tytu.mongodb.net/?retryWrites=true&w=majority"
|
const MONGO_URL = `mongodb+srv://FladDev:${MONGO_PASSWORD}@flad.mliekr2.mongodb.net/?retryWrites=true&w=majority`;
|
||||||
mongoose.connect(MONGO_URL)
|
mongoose.connect(MONGO_URL)
|
||||||
.then(() => console.log("Connect to MongoDB database successfully"))
|
.then(() => console.log("Connect to MongoDB database successfully"))
|
||||||
.catch(error => console.log("Error connecting : " + error));
|
.catch(error => console.log("Error connecting : " + error));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
@ -1,24 +1,22 @@
|
|||||||
import Controller from './interfaces/IController';
|
import IController from './interfaces/IController';
|
||||||
import { Router, Request, Response, NextFunction } from 'express';
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
import HttpException from '../exception/HttpException';
|
import HttpException from '../exception/HttpException';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import qs from 'qs';
|
import qs from 'qs';
|
||||||
|
|
||||||
class SpotifyController implements Controller {
|
class SpotifyController implements IController {
|
||||||
public path = '/spotify';
|
public path = '/spotify';
|
||||||
public router = Router();
|
public router = Router();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
console.log("useeeee");
|
|
||||||
|
|
||||||
this.initialiseRoutes();
|
this.initialiseRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
initialiseRoutes() {
|
initialiseRoutes() {
|
||||||
this.router.get(`${this.path}/exchange`,this.login);
|
this.router.get(`${this.path}/exchange`,this.login);
|
||||||
this.router.get(`${this.path}/callback`,this.getAccessToken);
|
this.router.get(`${this.path}/callback`,this.getAccessToken);
|
||||||
this.router.get(`${this.path}/refresh`,this.getRefreshToken);
|
this.router.get(`${this.path}/refresh`,this.getRefreshToken);
|
||||||
this.router.get(`${this.path}/spot`, this.getSpot);
|
this.router.get(`${this.path}/spot`, this.getSpot);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,254 +0,0 @@
|
|||||||
import { Router, Request, Response, NextFunction, RequestHandler } from 'express';
|
|
||||||
import Controller from './interfaces/IController';
|
|
||||||
import HttpException from '../exception/HttpException';
|
|
||||||
// import LocationService from '../../service/LocationService';
|
|
||||||
import IUser from '../models/User';
|
|
||||||
import UserService from '../services/UserService';
|
|
||||||
import validator from '../database/User/UserValidation'
|
|
||||||
import validationMiddleware from '../middlewares/validation/ValidatorMiddleware';
|
|
||||||
import authenticator from '../middlewares/authMiddleware'
|
|
||||||
import LocationService from '../services/LocationService';
|
|
||||||
class UserController implements Controller {
|
|
||||||
public path = '/users';
|
|
||||||
public router = Router();
|
|
||||||
private userService = new UserService();
|
|
||||||
private locationService = new LocationService();
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.initialiseRoutes();
|
|
||||||
}
|
|
||||||
|
|
||||||
private initialiseRoutes(): void {
|
|
||||||
this.router.post(
|
|
||||||
`${this.path}/register`,
|
|
||||||
validationMiddleware(validator.register),
|
|
||||||
this.register
|
|
||||||
);
|
|
||||||
this.router.post(
|
|
||||||
`${this.path}/login`,
|
|
||||||
validationMiddleware(validator.login),
|
|
||||||
this.login
|
|
||||||
);
|
|
||||||
this.router.get(`${this.path}`, authenticator, this.getUser);
|
|
||||||
this.router.get(`${this.path}/nextTo`, authenticator, this.getUserNext);
|
|
||||||
|
|
||||||
|
|
||||||
// //create
|
|
||||||
// this.router.post(`${this.path}`,this.createUser);
|
|
||||||
|
|
||||||
// // // get One
|
|
||||||
// this.router.get (`${this.path}/:userId`, this.getUserById);
|
|
||||||
// // // get All
|
|
||||||
// this.router.get (`${this.path}`, this.getAllUsers);
|
|
||||||
// //update One
|
|
||||||
// this.router.put (`${this.path}/:userId`, this.updateUser);
|
|
||||||
// //Delete One
|
|
||||||
// this.router.delete (`${this.path}/:userId`, this.deleteUser);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// private createUser = async (
|
|
||||||
// req: Request,
|
|
||||||
// res: Response,
|
|
||||||
// next: NextFunction
|
|
||||||
// ): Promise<Response | void> => {
|
|
||||||
// try {
|
|
||||||
|
|
||||||
// console.log(req.body);
|
|
||||||
// const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
|
||||||
// checkIfIsValidCreateTaskReqBody(reqBody);
|
|
||||||
// await this.userService.createUserById(reqBody.fin
|
|
||||||
// );
|
|
||||||
|
|
||||||
// res.status(200).send({ status: "Success", msg: "Success add" });
|
|
||||||
|
|
||||||
|
|
||||||
// } catch (error) {
|
|
||||||
// next(new HttpException(400, 'Cannot create post'));
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// private readonly getUserById: RequestHandler = async (
|
|
||||||
// req: Request,
|
|
||||||
// res: Response,
|
|
||||||
// next: NextFunction
|
|
||||||
// ): Promise<Response | void> => {
|
|
||||||
// try {
|
|
||||||
// const id = req.params.taskId;
|
|
||||||
// const userId = req.params.userId;
|
|
||||||
|
|
||||||
// const data = await this.userService.getUserById(id, userId);
|
|
||||||
// res.status(201).send(data);
|
|
||||||
|
|
||||||
// }
|
|
||||||
// catch(error){
|
|
||||||
// next(new HttpException(400, 'Cannot create post'));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
// private readonly getAllUsers: RequestHandler = async (
|
|
||||||
// req: Request,
|
|
||||||
// res: Response,
|
|
||||||
// next: NextFunction
|
|
||||||
// ): Promise<Response | void> => {
|
|
||||||
// try {
|
|
||||||
// const userId = req.params.userId;
|
|
||||||
// const tasks = await this.userService.getUsers(userId);
|
|
||||||
// const responseList = tasks.map(task => new TaskResumedRes(task));
|
|
||||||
// res.status(201).send(responseList);
|
|
||||||
|
|
||||||
// }
|
|
||||||
// catch(error){
|
|
||||||
// next(new HttpException(400, 'Cannot get user task'));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private deleteUser = async (
|
|
||||||
// req: Request,
|
|
||||||
// res: Response,
|
|
||||||
// next: NextFunction
|
|
||||||
// ): Promise<Response | void> => {
|
|
||||||
// try {
|
|
||||||
// const id = req.params.taskId;
|
|
||||||
// const userId = req.params.userId;
|
|
||||||
// await this.userService.DeleteUser(id, userId);
|
|
||||||
// return res.status(200).send({ status: "Success", msg: "Data Removed" });
|
|
||||||
// } catch (error) {
|
|
||||||
// next(new HttpException(400, 'Cannot create post'));
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// private updateUser = async (
|
|
||||||
// req: Request,
|
|
||||||
// res: Response,
|
|
||||||
// next: NextFunction
|
|
||||||
// ): Promise<Response | void> => {
|
|
||||||
// try {
|
|
||||||
|
|
||||||
// const taskId = req.params.taskId;
|
|
||||||
// const userId = req.params.userId;
|
|
||||||
// const reqBody:CreateTaskReqBody = Object.assign({}, req.body);
|
|
||||||
|
|
||||||
// const updatedTask = await this.userService.UpdateTask(
|
|
||||||
// // req.auth!.uid,
|
|
||||||
// taskId,
|
|
||||||
// userId,
|
|
||||||
// // firebase.auth().currentUser.getIdToken()
|
|
||||||
// reqBody.nom,
|
|
||||||
// reqBody.description,
|
|
||||||
// reqBody.logo,
|
|
||||||
// reqBody.duration,
|
|
||||||
// reqBody.done,
|
|
||||||
// // reqBody.tags,
|
|
||||||
// reqBody.repepat,
|
|
||||||
// reqBody.deb,
|
|
||||||
// reqBody.fin
|
|
||||||
// );
|
|
||||||
// // res.send('Success add');
|
|
||||||
// // res.status(201).json({ task });
|
|
||||||
// res.status(204).send(`Update a new contact: ${updatedTask}`);
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(error);
|
|
||||||
// next(new HttpException(403, 'Cannot create post'));
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
|
|
||||||
private register = async (
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> => {
|
|
||||||
try {
|
|
||||||
// the FladId should be created by the Userservice
|
|
||||||
const { name, email, password , idFlad, idSpotify } = req.body;
|
|
||||||
console.log(name, email, password, idFlad, idSpotify);
|
|
||||||
|
|
||||||
const token = await this.userService.register(
|
|
||||||
name,
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
idFlad,
|
|
||||||
idSpotify
|
|
||||||
);
|
|
||||||
|
|
||||||
res.status(201).json({ token });
|
|
||||||
} catch (error : any) {
|
|
||||||
next(new HttpException(400, error.message));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private login = async (
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> => {
|
|
||||||
try {
|
|
||||||
const { email, password } = req.body;
|
|
||||||
|
|
||||||
const token = await this.userService.login(email, password);
|
|
||||||
|
|
||||||
res.status(200).json({ token });
|
|
||||||
} catch (error : any) {
|
|
||||||
next(new HttpException(400, error.message));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private getUser = (
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Response | void => {
|
|
||||||
if (!req.user) {
|
|
||||||
return next(new HttpException(404, 'No logged in user'));
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).send({ data: req.user });
|
|
||||||
};
|
|
||||||
|
|
||||||
private getUserNext = async (
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
): Promise<Response | void> => {
|
|
||||||
try {
|
|
||||||
const longitude = Number(req.query.longitude);
|
|
||||||
const latitude = Number(req.query.latitude);
|
|
||||||
//verify::val_int(){
|
|
||||||
console.log('woooooooooooooo' + req);
|
|
||||||
if (isNaN(longitude) || isNaN(latitude)) {
|
|
||||||
console.log('============' + longitude)
|
|
||||||
console.log('============' + latitude)
|
|
||||||
console.log('Impossible de convertir la chaîne en nombre');
|
|
||||||
}
|
|
||||||
//}
|
|
||||||
const userId = req.user.idFlad;
|
|
||||||
const musicId = String(req.query.currentMusic);
|
|
||||||
console.log('============' + longitude)
|
|
||||||
console.log('============' + latitude)
|
|
||||||
console.log('daaaaaaaaaaaaaaaaaaaaaa' + musicId);
|
|
||||||
const data = await this.locationService.getNearUser(userId,musicId,latitude,longitude);
|
|
||||||
console.log(data);
|
|
||||||
res.status(201).send(data);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(error : any){
|
|
||||||
next(new HttpException(400, 'Cannot create get netUser'));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export default UserController;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
namespace Express {
|
|
||||||
export interface Request {
|
|
||||||
user: IUser;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,111 @@
|
|||||||
|
import { Router, Request, Response, NextFunction, RequestHandler } from 'express';
|
||||||
|
import IController from './interfaces/IController';
|
||||||
|
import HttpException from '../exception/HttpException';
|
||||||
|
import User from '../models/User';
|
||||||
|
import UserService from '../services/UserService';
|
||||||
|
import validator from '../middlewares/UserValidation'
|
||||||
|
import validationMiddleware from '../middlewares/validationMiddleware';
|
||||||
|
import authenticator from '../middlewares/authMiddleware'
|
||||||
|
import LocationService from '../services/LocationService';
|
||||||
|
|
||||||
|
class UserController implements IController {
|
||||||
|
public path = '/users';
|
||||||
|
public router = Router();
|
||||||
|
private userService = new UserService();
|
||||||
|
private locationService = new LocationService();
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.initRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
private initRoutes(): void {
|
||||||
|
this.router.post(
|
||||||
|
`${this.path}/register`,
|
||||||
|
validationMiddleware(validator.register),
|
||||||
|
this.register
|
||||||
|
);
|
||||||
|
this.router.post(
|
||||||
|
`${this.path}/login`,
|
||||||
|
validationMiddleware(validator.login),
|
||||||
|
this.login
|
||||||
|
);
|
||||||
|
this.router.get(`${this.path}`, authenticator, this.getUser);
|
||||||
|
this.router.get(`${this.path}/nextTo`, authenticator, this.getUserNext);
|
||||||
|
}
|
||||||
|
|
||||||
|
private register = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<Response | void> => {
|
||||||
|
try {
|
||||||
|
const { name, email, password, idSpotify } = req.body;
|
||||||
|
const token = await this.userService.register(
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
idSpotify
|
||||||
|
);
|
||||||
|
res.status(201).json({ token });
|
||||||
|
} catch (error: any) {
|
||||||
|
next(new HttpException(400, error.message));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private login = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<Response | void> => {
|
||||||
|
try {
|
||||||
|
const { email, password } = req.body;
|
||||||
|
const token = await this.userService.login(email, password);
|
||||||
|
res.status(200).json({ token });
|
||||||
|
} catch (error: any) {
|
||||||
|
next(new HttpException(400, error.message));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private getUser = (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Response | void => {
|
||||||
|
if (!req.user) {
|
||||||
|
return next(new HttpException(404, 'No logged in user'));
|
||||||
|
}
|
||||||
|
res.status(200).send({ data: req.user });
|
||||||
|
};
|
||||||
|
|
||||||
|
private getUserNext = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<Response | void> => {
|
||||||
|
try {
|
||||||
|
const longitude = Number(req.query.longitude);
|
||||||
|
const latitude = Number(req.query.latitude);
|
||||||
|
if (isNaN(longitude) || isNaN(latitude)) {
|
||||||
|
console.log('Unable to convert string to number');
|
||||||
|
throw new Error('Unable to convert string to number');
|
||||||
|
}
|
||||||
|
const userId = req.user.id;
|
||||||
|
const musicId = String(req.query.currentMusic);
|
||||||
|
const data = await this.locationService.getNearUser(userId, musicId, latitude, longitude);
|
||||||
|
res.status(201).send(data);
|
||||||
|
}
|
||||||
|
catch (error: any) {
|
||||||
|
next(new HttpException(400, 'Cannot create get netUser: ' + error.message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserController;
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
namespace Express {
|
||||||
|
export interface Request {
|
||||||
|
user: User;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
import { Schema } from 'mongoose';
|
|
||||||
|
|
||||||
interface IToken extends Object {
|
|
||||||
id: Schema.Types.ObjectId;
|
|
||||||
expiresIn: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default IToken;
|
|
@ -1,8 +0,0 @@
|
|||||||
import { Schema, model } from 'mongoose';
|
|
||||||
|
|
||||||
const notificationSchema = new Schema({
|
|
||||||
type: {type: String, required: true},
|
|
||||||
content: {type: String, required: true}
|
|
||||||
});
|
|
||||||
|
|
||||||
export default {Notification: model("nofitication", notificationSchema)}
|
|
@ -1,59 +1,21 @@
|
|||||||
export interface Position {
|
import { Document } from 'mongoose';
|
||||||
/**
|
|
||||||
* Creation timestamp for coords
|
|
||||||
*/
|
|
||||||
timestamp: number;
|
|
||||||
/**
|
|
||||||
* The GPS coordinates along with the accuracy of the data
|
|
||||||
*/
|
|
||||||
coords: {
|
|
||||||
/**
|
|
||||||
* Latitude in decimal degrees
|
|
||||||
*/
|
|
||||||
latitude: number;
|
|
||||||
/**
|
|
||||||
* longitude in decimal degrees
|
|
||||||
*/
|
|
||||||
longitude: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
export class PlacePosition implements Position {
|
|
||||||
timestamp: number;
|
|
||||||
coords: {
|
|
||||||
latitude: number;
|
|
||||||
longitude: number;
|
|
||||||
};
|
|
||||||
constructor(timestamp: number,latitude : number ,longitude: number){
|
|
||||||
this.timestamp = timestamp;
|
|
||||||
this.coords = {latitude, longitude};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class UserLocation {
|
export class UserLocation {
|
||||||
uuid: string;
|
userId: string;
|
||||||
|
musicId : string;
|
||||||
latitude : number;
|
latitude : number;
|
||||||
longitude: number;
|
longitude: number;
|
||||||
musicId : string;
|
constructor(userId: string, musicId : string,latitude: number, longitude: number){
|
||||||
constructor(uuid: string, musicId : string,latitude: number, longitude: number){
|
this.userId = userId;
|
||||||
this.uuid = uuid;
|
|
||||||
this.musicId = musicId;
|
this.musicId = musicId;
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Place{
|
export class Location extends Document {
|
||||||
position: Position;
|
userId: string;
|
||||||
address: Address;
|
musicId: string;
|
||||||
constructor(address: Address,position: Position){
|
latitude: number;
|
||||||
this.position = position;
|
longitude: number;
|
||||||
this.address = address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export type Address = {
|
|
||||||
street : string;
|
|
||||||
city : string;
|
|
||||||
state : string;
|
|
||||||
zip : string;
|
|
||||||
}
|
}
|
@ -1,27 +1,6 @@
|
|||||||
import jwt from 'jsonwebtoken';
|
import { Schema } from 'mongoose';
|
||||||
import IUser from './User';
|
|
||||||
import IToken from '../database/IToken';
|
|
||||||
|
|
||||||
export const createToken = (user: IUser): string => {
|
export default interface Token extends Object {
|
||||||
return jwt.sign({ id: user._id }, "foo" as jwt.Secret, {
|
id: Schema.Types.ObjectId;
|
||||||
expiresIn: '100d',
|
expiresIn: number;
|
||||||
});
|
}
|
||||||
};
|
|
||||||
|
|
||||||
export const verifyToken = async (
|
|
||||||
token: string
|
|
||||||
): Promise<jwt.VerifyErrors | IToken> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
jwt.verify(
|
|
||||||
token,
|
|
||||||
"foo" as jwt.Secret,
|
|
||||||
(err, payload) => {
|
|
||||||
if (err) return reject(err);
|
|
||||||
|
|
||||||
resolve(payload as IToken);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default { createToken, verifyToken };
|
|
@ -1,139 +1,58 @@
|
|||||||
// import db from '../database';
|
import { UserLocation } from '../models/Location';
|
||||||
import { Place, PlacePosition, Position, UserLocation } from '../models/Location';
|
|
||||||
import axios from 'axios';
|
|
||||||
import LocationSchema from "../database/LocationSchema";
|
import LocationSchema from "../database/LocationSchema";
|
||||||
|
|
||||||
class LocationService {
|
class LocationService {
|
||||||
private locationCollection = LocationSchema;
|
private locations = LocationSchema;
|
||||||
// private API_KEY : string = "AIzaSyBFCEAtmhZ8jvw84UTQvX3Aqpr66GVqB_A";
|
public async getNearUser(userId: string, musicId: string, latitude: number, longitude: number) {
|
||||||
public async getNearUser(idFlad : string, musicId : string,latitude : number, longitude : number)
|
await this.locations.findOneAndUpdate(
|
||||||
{
|
{ userId },
|
||||||
await this.locationCollection.findOneAndUpdate(
|
{ userId, musicId, latitude, longitude },
|
||||||
{ idFlad },
|
|
||||||
{ idFlad, musicId, latitude, longitude },
|
|
||||||
{ upsert: true }
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
const snapshot = await this.locationCollection.find({ idFlad: { $ne: idFlad } });
|
const snapshot = await this.locations.find({ userId: { $ne: userId } });
|
||||||
if (snapshot.length === 0) {
|
if (!snapshot.length) {
|
||||||
console.log('No matching documents.');
|
console.log('No matching documents.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dbUsersList:UserLocation[] = [];
|
let usersLocation: UserLocation[] = [];
|
||||||
snapshot.forEach(doc => {
|
snapshot.forEach(doc => {
|
||||||
dbUsersList.push(new UserLocation(doc.idFlad,doc.musicId,doc.latitude,doc.longitude));
|
usersLocation.push(new UserLocation(doc.userId, doc.musicId, doc.latitude, doc.longitude));
|
||||||
console.log(doc.idFlad, '=>', doc);
|
|
||||||
});
|
});
|
||||||
// missing the curent music
|
const listUser: Record<string, string> = {};
|
||||||
let listUser: string[] = [];
|
usersLocation.forEach(user => {
|
||||||
const listUser2: Record<string, string> = {};
|
const distance = this.distanceBetween(latitude, longitude, user.latitude, user.longitude);
|
||||||
dbUsersList.forEach(user => {
|
if (distance <= 100) {
|
||||||
console.log(user);
|
listUser[user.userId] = user.musicId;
|
||||||
const dist = this.distanceBetween(latitude , longitude , user.latitude, user.longitude);
|
}
|
||||||
console.log(user.uuid,dist);
|
});
|
||||||
if (dist <= 100) {
|
return { listUser };
|
||||||
|
|
||||||
listUser.push(user.uuid);
|
|
||||||
listUser2[user.uuid] = user.musicId;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
return{ listUser, listUser2};
|
|
||||||
// $listUser[] = {userID,idMusic};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCenter (points: Position[]) {
|
private distanceBetween(lat1: number, lon1: number, lat2: number, lon2: number): number {
|
||||||
if (Array.isArray(points) === false || points.length === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const numberOfPoints = points.length;
|
|
||||||
|
|
||||||
const sum = points.reduce(
|
|
||||||
(acc, point) => {
|
|
||||||
const pointLat = this.toRad(point.coords.latitude);
|
|
||||||
const pointLon = this.toRad(point.coords.longitude);
|
|
||||||
return {
|
|
||||||
X: acc.X + Math.cos(pointLat) * Math.cos(pointLon),
|
|
||||||
Y: acc.Y + Math.cos(pointLat) * Math.sin(pointLon),
|
|
||||||
Z: acc.Z + Math.sin(pointLat),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{ X: 0, Y: 0, Z: 0 }
|
|
||||||
);
|
|
||||||
|
|
||||||
const X = sum.X / numberOfPoints;
|
|
||||||
const Y = sum.Y / numberOfPoints;
|
|
||||||
const Z = sum.Z / numberOfPoints;
|
|
||||||
|
|
||||||
return {
|
|
||||||
longitude: this.toDeg(Math.atan2(Y, X)),
|
|
||||||
latitude: this.toDeg(Math.atan2(Z, Math.sqrt(X * X + Y * Y))),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
public toRad = (value: number) => (value * Math.PI) / 180;
|
|
||||||
public toDeg = (value: number) => (value * 180) / Math.PI;
|
|
||||||
|
|
||||||
// sa c'est un utils du coup mettre dans une calss utils
|
|
||||||
// resulta en km
|
|
||||||
private distanceBetween (lat1 : number, lon1 : number, lat2: number, lon2 : number) : number {
|
|
||||||
if ((lat1 == lat2) && (lon1 == lon2)) {
|
if ((lat1 == lat2) && (lon1 == lon2)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var radlat1 = Math.PI * lat1/180;
|
var radlat1 = Math.PI * lat1 / 180;
|
||||||
var radlat2 = Math.PI * lat2/180;
|
var radlat2 = Math.PI * lat2 / 180;
|
||||||
var theta = lon1-lon2;
|
var theta = lon1 - lon2;
|
||||||
var radtheta = Math.PI * theta/180;
|
var radtheta = Math.PI * theta / 180;
|
||||||
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
|
||||||
|
|
||||||
if (dist > 1) {
|
if (dist > 1) {
|
||||||
dist = 1;
|
dist = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
dist = Math.acos(dist);
|
dist = Math.acos(dist);
|
||||||
dist = dist * 180/Math.PI;
|
dist = dist * 180 / Math.PI;
|
||||||
dist = dist * 60 * 1.1515;
|
dist = dist * 60 * 1.1515;
|
||||||
dist = dist * 1.609344;
|
dist = dist * 1.609344;
|
||||||
|
|
||||||
return dist;
|
return dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private distanceBetweenPosition(first : Position, second : Position) : number {
|
|
||||||
return this.distanceBetween (first.coords.latitude, first.coords.longitude, second.coords.latitude, second.coords.longitude)
|
|
||||||
}
|
|
||||||
|
|
||||||
// give a array of position sorted by distance and return the first
|
|
||||||
private findNearest(main : Position, list : Position[]){
|
|
||||||
const nearest = this.orderByDistance(main, list)[0]
|
|
||||||
return nearest;
|
|
||||||
}
|
|
||||||
|
|
||||||
//distanceFn: DistanceFn = getDistance est param sa serrait cool de lui passer un fonction
|
|
||||||
private orderByDistance (mainPos: Position,coords: Position[]){
|
|
||||||
return coords
|
|
||||||
.slice()
|
|
||||||
.sort((a, b) => this.distanceBetweenPosition(mainPos, a) - this.distanceBetweenPosition(mainPos, b));
|
|
||||||
};
|
|
||||||
|
|
||||||
// getCenter(coords)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default LocationService;
|
||||||
|
|
||||||
export default LocationService;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
|||||||
|
import jwt from 'jsonwebtoken';
|
||||||
|
import User from '../models/User';
|
||||||
|
import Token from '../models/Token';
|
||||||
|
|
||||||
|
export const createToken = (user: User): string => {
|
||||||
|
return jwt.sign({ id: user._id }, "dave" as jwt.Secret, {
|
||||||
|
expiresIn: '1d',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const verifyToken = async (
|
||||||
|
token: string
|
||||||
|
): Promise<jwt.VerifyErrors | Token> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
jwt.verify(
|
||||||
|
token,
|
||||||
|
"dave" as jwt.Secret,
|
||||||
|
(err, payload) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
resolve(payload as Token);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { createToken, verifyToken };
|
Loading…
Reference in new issue