Skip to content

Commit 6b1c547

Browse files
committed
Updating Due Date and Finish Contract APIs
1 parent 5be3849 commit 6b1c547

File tree

6 files changed

+3641
-1523
lines changed

6 files changed

+3641
-1523
lines changed

app.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ app.listen(PORT, () => {
3737
console.log("Server is running on PORT:- " + PORT);
3838
})
3939

40+
mongoose.set("strictQuery", true);
4041
mongoose.connect(process.env.MONGO_URL).then(() => {
4142
console.log("Connected to Database");
4243
}).catch((err) => {

controllers/contractController.js

+117-53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Chat = require("../models/chatModel");
22
const Contract = require("../models/contractModel");
3-
const User = require("../models/userModel");
43
const catchAsync = require("../utils/catchAsync");
4+
const AppError = require('../utils/appError')
55

66
exports.getAllContracts = catchAsync(async (req, res) => {
77
const contracts = await Contract.find()
@@ -26,19 +26,19 @@ exports.getUserContracts = catchAsync(async (req, res) => {
2626
.populate("team.member", "name photo")
2727
.sort("-createdAt");
2828

29-
const date = new Date();
29+
const date = new Date();
3030

31-
// we get an array
31+
// we get an array
3232
// iterate through every contract
3333
// if in-progress -> compare due date and current date and update to `delayed` if required
3434

35-
contracts.forEach(async contract => {
35+
contracts.forEach(async (contract) => {
3636
const contractDueDate = contract.dueDate;
37-
if (contract.status === 'in-progress') {
37+
if (contract.status === "in-progress") {
3838
if (date >= contractDueDate) {
3939
let updatedContract = await Contract.findByIdAndUpdate(contract.id, {
40-
status: 'delayed'
41-
})
40+
status: "delayed",
41+
});
4242
contract = updatedContract;
4343
}
4444
}
@@ -58,41 +58,41 @@ exports.getContract = catchAsync(async (req, res) => {
5858
.populate("lead", "name photo")
5959
.populate("team.member", "name photo");
6060

61-
// chek if contract is in-progress
62-
// if in-progress --> compare due date and current date and update to `delayed` if required
61+
// chek if contract is in-progress
62+
// if in-progress --> compare due date and current date and update to `delayed` if required
6363

64-
const contractDueDate = contract.dueDate;
65-
console.log(contract.dueDate);
64+
const contractDueDate = contract.dueDate;
65+
console.log(contract.dueDate);
66+
67+
let date = new Date();
68+
69+
console.log(date);
70+
console.log(date >= contractDueDate);
71+
console.log(typeof date);
72+
console.log(typeof contractDueDate);
73+
74+
if (contract.status === "in-progress") {
75+
if (contractDueDate <= date) {
76+
const updatedContract = await Contract.findByIdAndUpdate(contract.id, {
77+
status: "delayed",
78+
});
6679

67-
let date = new Date();
68-
69-
console.log(date);
70-
console.log(date >= contractDueDate);
71-
console.log(typeof(date));
72-
console.log(typeof(contractDueDate));
73-
74-
if (contract.status === 'in-progress') {
75-
if (contractDueDate <= date) {
76-
const updatedContract = await Contract.findByIdAndUpdate(contract.id, {
77-
status: 'delayed'
78-
})
79-
80-
res.status(200).json({
81-
status: 'success',
82-
updatedContract
83-
})
84-
}
85-
} else {
8680
res.status(200).json({
8781
status: "success",
88-
contract
82+
updatedContract,
8983
});
9084
}
85+
} else {
86+
res.status(200).json({
87+
status: "success",
88+
contract,
89+
});
90+
}
9191
});
9292

9393
exports.initializeContract = catchAsync(async (req, res) => {
94-
95-
const { team, startDate, dueDate, contractName, projectDescription, chatId } = req.body;
94+
const { team, startDate, dueDate, contractName, projectDescription, chatId } =
95+
req.body;
9696
const userId = req.user.id;
9797

9898
const contract = await Contract.create({
@@ -148,7 +148,6 @@ exports.initializeContract = catchAsync(async (req, res) => {
148148
});
149149

150150
exports.acceptContract = catchAsync(async (req, res) => {
151-
152151
const userId = req.user._id;
153152
const { chatId } = req.body;
154153

@@ -204,9 +203,8 @@ exports.acceptContract = catchAsync(async (req, res) => {
204203
});
205204

206205
exports.denyContract = catchAsync(async (req, res) => {
207-
208206
const contractId = req.params.contractId;
209-
const userId = req.user.id
207+
const userId = req.user.id;
210208

211209
const deniedContract = await Contract.updateOne(
212210
{
@@ -230,14 +228,34 @@ exports.denyContract = catchAsync(async (req, res) => {
230228
res.send(updatedContract);
231229
});
232230

233-
exports.updateContract = catchAsync(async (req, res) => {
234-
231+
exports.updateDueContract = catchAsync(async (req, res, next) => {
235232
const { newDueDate, reason } = req.body;
236233
const contractId = req.params.contractId;
237-
234+
238235
const contract = await Contract.findById(contractId);
236+
237+
238+
let date = new Date();
239+
240+
console.log(Date.parse(newDueDate) > Date.parse(date));
241+
242+
const parsedNewDueDate = Date.parse(newDueDate);
243+
const parsedCurrentDate = Date.parse(date)
244+
const parsedStartingDate = Date.parse(contract.startDate)
245+
const parsedDueDate = Date.parse(contract.dueDate)
246+
247+
console.log(parsedDueDate);
248+
console.log(parsedNewDueDate);
249+
250+
// check if prevDueDate and newDueDate arent similar
251+
if (parsedNewDueDate === parsedDueDate) {
252+
return next(new AppError("Enter a new Due Date"));
253+
}
254+
255+
if (parsedNewDueDate < parsedCurrentDate || parsedNewDueDate < parsedStartingDate) {
256+
return next(new AppError("Enter a logical new Due Date 🙄"));
257+
}
239258

240-
console.log(contract.dueDate);
241259

242260
const newDueObj = {
243261
prevDate: contract.dueDate,
@@ -247,17 +265,63 @@ exports.updateContract = catchAsync(async (req, res) => {
247265
let newprevDueDatesArr = contract.prevDueDates;
248266
newprevDueDatesArr.push(newDueObj);
249267

250-
const updatedContract = await Contract.findByIdAndUpdate(
251-
contractId,
252-
{
253-
dueDate: newDueDate,
254-
prevDueDates: newprevDueDatesArr,
255-
},
256-
{
257-
new: true,
258-
runValidators: true,
259-
}
260-
);
268+
let updatedContract;
261269

262-
res.send(updatedContract);
263-
});
270+
if (contract.status === "delayed") {
271+
updatedContract = await Contract.findByIdAndUpdate(
272+
contractId,
273+
{
274+
dueDate: newDueDate,
275+
prevDueDates: newprevDueDatesArr,
276+
status: "in-progress",
277+
},
278+
{
279+
new: true,
280+
runValidators: true,
281+
}
282+
);
283+
} else {
284+
updatedContract = await Contract.findByIdAndUpdate(
285+
contractId,
286+
{
287+
dueDate: newDueDate,
288+
prevDueDates: newprevDueDatesArr,
289+
},
290+
{
291+
new: true,
292+
runValidators: true,
293+
}
294+
);
295+
}
296+
297+
res.status(200).json({
298+
status: "success",
299+
updatedContract,
300+
});
301+
});
302+
303+
exports.finishContract = catchAsync(async (req,res) => {
304+
const { githubLink, liveLink, projectImages } = req.body;
305+
const contractId = req.params.contractId;
306+
307+
if (!githubLink || !liveLink || !projectImages) {
308+
return next(new AppError('Provide sufficient details to finish the project'))
309+
}
310+
311+
const finishedContract = await Contract.findByIdAndUpdate(contractId, {
312+
githubLink,
313+
liveLink,
314+
projectImages
315+
})
316+
317+
res.status(200).json({
318+
status: 'success',
319+
finishedContract
320+
})
321+
})
322+
323+
// contractId -->
324+
325+
// n members -- (n-1)*n
326+
327+
// After Finishing review, contract will ask to revoiew the members

models/contractModel.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const contractModel = mongoose.Schema({
3232
default: false
3333
},
3434
review: {
35+
// ref data --> rating, description, userId, contractId, ratedUserId
3536
type: Number,
3637
default: 4.5,
3738
min: [1, 'Rating must be above 1.0'],
@@ -49,7 +50,10 @@ const contractModel = mongoose.Schema({
4950
},
5051
prevDueDates: [
5152
{
52-
prevDate: Date,
53+
prevDate: {
54+
type: Date,
55+
default: Date.now() + 172800000
56+
},
5357
delayReason: String
5458
}
5559
],
@@ -61,7 +65,21 @@ const contractModel = mongoose.Schema({
6165
chatId: {
6266
type: mongoose.Schema.Types.ObjectId,
6367
ref: 'Chat',
64-
}
68+
},
69+
githubLink: {
70+
type: String,
71+
default: ''
72+
},
73+
liveLink: {
74+
type: String,
75+
default: ''
76+
},
77+
projectImages: [
78+
{
79+
type: String,
80+
default: ''
81+
}
82+
]
6583
},
6684
{ timestamps: true }
6785
)

0 commit comments

Comments
 (0)