Skip to content
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
2 changes: 1 addition & 1 deletion backend/src/controllers/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Job {

async getJobsByCustomerId(req: Request, res: Response, next: NextFunction) {
try {
const jobs = await this.userService.getJobsByCustomerId(+req.params.id);
const jobs = await this.userService.getJobsByCustomerId(+req.params.id,+req.query.skip,+req.query.limit);
res.json(jobs);
} catch (e) {
return next(e);
Expand Down
40 changes: 37 additions & 3 deletions backend/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,48 @@ class User {
}
}

async getCustomer(req: Request, res: Response, next: NextFunction) {
async createCustomer(req: Request, res: Response, next: NextFunction) {
try {
const users = await this.userService.getCustomer(+req.query.id);
const response = await this.userService.createCustomer(req.body);
res.json(response);
} catch (e) {
return next(e);
}
}
async updateCustomerById(req: Request, res: Response, next: NextFunction) {
try {
const response = await this.userService.updateCustomerById(+req.params.id,req.body);
res.json(response);
} catch (e) {
return next(e);
}
}
async getCustomerById(req: Request, res: Response, next: NextFunction) {
try {
const users = await this.userService.getCustomerById(+req.params.id);
res.json(users);
} catch (e) {
return next(e);
}
}
}

async getCustomerComments(req: Request, res: Response, next: NextFunction) {
try {
const customerComments = await this.userService.getCustomerComments(+req.params.id,+req.query.skip,+req.query.limit);
res.json(customerComments);
} catch (e) {
return next(e);
}
}

async createCommentForCustomer(req: Request, res: Response, next: NextFunction) {
try {
const response = await this.userService.createCommentForCustomer(+req.params.id,req.body);
res.json(response);
} catch (e) {
return next(e);
}
}
}

export default User;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const commentsSchema = new mongoose.Schema({
comment:mongoose.Schema.Types.String,
score: mongoose.Schema.Types.Number,
subs_came: mongoose.Schema.Types.Number,
});
},{timestamps: true});

export const Comments = mongoose.model<CommentsDocumnet>('Comments', commentsSchema);
49 changes: 33 additions & 16 deletions backend/src/models/Customer.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import mongoose, { Schema } from 'mongoose';
import { IOption } from './Blogger';

export type CustomerDocument = mongoose.Document & {
_id: number;
ig_id: number;
username: string;
full_name: string;
profile_picture: string;
biography: string;
website: string;
commentId: Number;
name: String;
surname: String;
profile_picture: String;
location: {
country: IOption;
city: IOption;
};
contact: {
mail: String;
phone: String;
link: String;
}
};

const customerSchema = new mongoose.Schema({
_id:mongoose.Schema.Types.Number,
ig_id:mongoose.Schema.Types.Number,
username:Schema.Types.String,
full_name:Schema.Types.String,
profile_picture:Schema.Types.String,
biography:Schema.Types.String,
website:Schema.Types.String,
commentId: {type:Number, ref: 'Comments'},
}, { timestamps: true, _id:false });
_id: mongoose.Schema.Types.Number,
name: Schema.Types.String,
surname: Schema.Types.String,
profile_picture: Schema.Types.String,
location:{
country:{
label:Schema.Types.String,
value:Schema.Types.String
},
city:{
label:Schema.Types.String,
value:Schema.Types.String
}
},
contact:{
mail: Schema.Types.String,
phone: Schema.Types.String,
link: Schema.Types.String,
}
}, { timestamps: true, _id: false });

export const Customer = mongoose.model<CustomerDocument>('Customers', customerSchema);
18 changes: 18 additions & 0 deletions backend/src/models/CustomerComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import mongoose from 'mongoose';


export type CommentsDocumnet = mongoose.Document & {
customerId:Number;
bloggerId:Number;
comment:String;
score:Number;
};

const commentsSchema = new mongoose.Schema({
customerId:{type: Number, ref: 'Customers'},
bloggerId:{type: Number, ref: 'Bloggers'},
comment:mongoose.Schema.Types.String,
score:mongoose.Schema.Types.Number,
},{timestamps: true});

export const Comments = mongoose.model<CommentsDocumnet>('CustomerComments', commentsSchema);
21 changes: 19 additions & 2 deletions backend/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@ user.post('/blogger/:id/comment', (req, res, next) => {
userControllerInstance.createCommentForBlogger(req, res, next);
});

user.get('/customer', (req, res, next) => {
userControllerInstance.getCustomer(req, res, next);
user.post('/customer', (req, res, next) => {
userControllerInstance.createCustomer(req, res, next);
});

user.get('/customer/:id', (req, res, next) => {
userControllerInstance.getCustomerById(req, res, next);
});

user.put('/customer/:id', (req, res, next) => {
userControllerInstance.updateCustomerById(req, res, next);
});

user.get('/customer/:id/comments', (req, res, next) => {
userControllerInstance.getCustomerComments(req, res, next);
});

user.post('/customer/:id/comment', (req, res, next) => {
userControllerInstance.createCommentForCustomer(req, res, next);
});


export default user;
6 changes: 3 additions & 3 deletions backend/src/services/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ICreateJobBody{

export interface IJobService{
createJob: (id: number, body: ICreateJobBody) => void;
getJobsByCustomerId: (id: number) => Promise<JobsDocumnet[]>;
getJobsByCustomerId: (id: number, skip: number, limit: number) => Promise<JobsDocumnet[]>;
getAllJobs: () => Promise<JobsDocumnet[]>;
getJobById: (id:string) => Promise<JobsDocumnet>;
updateStatusJobById: (id:string,status:boolean) => Promise<JobsDocumnet>;
Expand Down Expand Up @@ -47,8 +47,8 @@ class JobService implements IJobService {
await job.save();
}

async getJobsByCustomerId(id: number) {
return Jobs.find({customerId:id});
async getJobsByCustomerId(id: number, skip: number, limit: number) {
return Jobs.find({customerId:id}).skip(+skip).limit(+limit);
}

async getAllJobs() {
Expand Down
100 changes: 74 additions & 26 deletions backend/src/services/user.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
import {Customer,CustomerDocument} from '../models/Customer';
import {Blogger,BloggerDocument} from '../models/Blogger';
import {Comments,CommentsDocumnet} from '../models/Comments';
import {Comments as BlogerComments, CommentsDocumnet as BloggerCD} from '../models/BloggerComments';
import {Comments as CustomerComments, CommentsDocumnet as CustomerCD} from '../models/CustomerComments';

export interface ICreateCommentBody{
export interface ICreateCommentBlogger{
senderId: Number;
comment: String;
score: Number;
subs_came: Number;
}

export interface IAverageData {
export interface ICreateCommentCustomer{
senderId: Number;
comment: String;
score: Number;
}

export interface IAverageDataBlogger {
averageComing: number;
averageScore: number;
}

export interface IUserService{
getBlogger: (id: number) => Promise<BloggerDocument>;
createCommentForBlogger: (id: number, body: ICreateCommentBody) => object;
getBloggerComments: (id:number,skip: number,limit: number) => Promise<{ comments: CommentsDocumnet[]; averageData: IAverageData; }>;
getCustomer: (id: number) => Promise<CustomerDocument>;
createCommentForBlogger: (id: number, body: ICreateCommentBlogger) => object;
getBloggerComments: (id: number,skip: number,limit: number) => Promise<{ comments: BloggerCD[]; averageData: IAverageDataBlogger; }>;
createCustomer: (data: CustomerDocument) => Promise<CustomerDocument>;
updateCustomerById: (_id: number, data: CustomerDocument) => Promise<CustomerDocument>;
getCustomerById: (id: number) => Promise<CustomerDocument>;
createCommentForCustomer: (id: number, body: ICreateCommentCustomer) => Promise<any>
getCustomerComments: (id: number, skip: number,limit: number) => Promise <{comments: CustomerCD[],averageScore: Number}>;
}

async function calculateAverageData (id:number,skip:number): Promise<IAverageData> {
if(!skip){
let averageComing: number;
let averageScore: number;
const allCommnets = await Comments.find({bloggerId:id});
const subsCame = allCommnets.map(e => e.subs_came);
const countSubs:any = subsCame.reduce((a:number, b:number) => a + b, 0);
averageComing = Math.round(countSubs / subsCame.length);

const score = allCommnets.map(e => e.score);
const countScore:any = score.reduce((a:number, b:number) => a + b, 0);
averageScore = Math.round(countScore / score.length);
return {averageComing,averageScore}
}
return null;
function callculateAvg (data:Number[]) {
const countData:any = data.reduce((a:number, b:number) => a + b, 0);
return Math.round(countData / data.length);
}

async function calculateAverageDataBlogger (id:number): Promise<IAverageDataBlogger> {
const allComments = await BlogerComments.find({bloggerId:id});
const subsCame = allComments.map(e => e.subs_came);
const score = allComments.map(e => e.score);
return {averageComing:callculateAvg(subsCame),averageScore:callculateAvg(score)};
}

async function calculateAverageScoreCustomer (id:number) {
const comments = await CustomerComments.find({customerId:id});
const score = comments.map(e => e.score);
return callculateAvg(score);
}

class UserService implements IUserService {
//Blogger Services
async getBlogger(id: number) {
return Blogger.findOne({ig_id:id});
}

async createCommentForBlogger(id:number,body:ICreateCommentBody){
const comment = new Comments({
async createCommentForBlogger(id:number,body:ICreateCommentBlogger){
const comment = new BlogerComments({
customerId:body.senderId,
bloggerId:id,
comment:body.comment,
Expand All @@ -56,15 +69,50 @@ class UserService implements IUserService {
}

async getBloggerComments(id:number,skip: number,limit: number){
const averageData = await calculateAverageData(id,skip);
const comments = await Comments.find({bloggerId:id}).populate('customerId').skip(+skip).limit(+limit);
const averageData = !skip ? await calculateAverageDataBlogger(id) : null;
const comments = await BlogerComments.find({bloggerId:id}).populate('customerId').skip(+skip).limit(+limit);
return {comments, averageData};
}

async getCustomer(id: number) {
//Customer Services
async createCustomer(data: CustomerDocument){
const lastCustomer = await Customer.findOne({}).sort({createdAt:-1});
const _id = lastCustomer ? lastCustomer._id + 1 : 1;
const newCustomer = new Customer({
_id ,
name: data.name,
surname: data.surname,
profile_picture: data.profile_picture,
location: data.location,
contact: data.contact
})
return await newCustomer.save();
}

async updateCustomerById(_id:number,data: CustomerDocument){
return await Customer.findOneAndUpdate({_id}, data, {new:true});
}

async getCustomerById(id: number) {
return Customer.findById(id);
}

async createCommentForCustomer(id:number,body:ICreateCommentBlogger){
const comment = new CustomerComments({
customerId: id,
bloggerId: body.senderId,
comment: body.comment,
score: body.score,
});
const newComment = await comment.save();
return await CustomerComments.findById(newComment._id).populate('bloggerId');
}

async getCustomerComments(id:number,skip: number,limit: number){
const averageScore = !skip ? await calculateAverageScoreCustomer(id) : null;
const comments = await CustomerComments.find({customerId:id}).sort({createdAt:-1}).populate('bloggerId').skip(+skip).limit(+limit);
return {comments, averageScore};
}
}

export default UserService;
Expand Down
61 changes: 61 additions & 0 deletions ui/src/app/components/Job/EditJob/attachments/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { attachmentsIconsCustomer } from '../../../../../img';
import { AttachmentsContainer } from './styles';
import { v4 as uuidv4 } from 'uuid';
import FileLoader from '../../../shared/file-loader';
import withTheme from '../../../../../HOC/withTheme';
import { defineType } from '../../../../helpers/define';
import { FILETYPES } from '../../../../../consts/lists';

const Attachments = props => {
const [attachments, setAttachments] = React.useState<
{ icon: string; file }[]
>([]);
const prevSaveFile = async file => {
const type = defineType(file.type);
const fileCopy = attachments.some(f => f.file.name === file.name);
if (type && !fileCopy) {
setAttachments([
...attachments,
{ icon: attachmentsIconsCustomer[type], file },
]);
}
};
return (
<AttachmentsContainer theme={props.theme}>
<span className="title">Attached media</span>
<div className="attached">
<div className="list">
{attachments.length > 0 &&
attachments.map(item => {
return (
<img
onClick={_ => {
setAttachments(
attachments.filter(f => f.file.name !== item.file.name),
);
}}
className="item"
src={item.icon}
alt="iconAtched"
key={uuidv4()}
/>
);
})}
</div>
<div className="add-file">
<FileLoader
id={'upload'}
handleSaveFile={file => prevSaveFile(file)}
allowedFormats={Object.values(FILETYPES)}
/>
<label htmlFor="upload">
<img src={attachmentsIconsCustomer.attachedButton} alt="add-file" />
</label>
</div>
</div>
</AttachmentsContainer>
);
};

export default withTheme(Attachments);
Loading