-
Notifications
You must be signed in to change notification settings - Fork 13
/
Mutation.js
131 lines (126 loc) Β· 3.17 KB
/
Mutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import bcrypt from "bcrypt";
import { generate } from "shortid";
import jwt from "jsonwebtoken";
export default {
async createAccount(
parent,
{ input: { name, username, password } },
{ customers }
) {
let existingCustomer = await customers.findOne({
username,
});
if (!existingCustomer) {
let hash = bcrypt.hashSync(password, 10);
let newCustomer = {
id: generate(),
name,
username,
currentPets: [],
checkoutHistory: [],
password: hash,
dateCreated: new Date().toISOString(),
};
await customers.insertOne(newCustomer);
return newCustomer;
} else {
throw new Error(
"An account with this username already exists."
);
}
},
async logIn(
parent,
{ username, password },
{ customers, currentCustomer }
) {
let customer = await customers.findOne({ username });
if (!customer) {
throw new Error(
`Account with that username: ${username} not found.`
);
}
if (bcrypt.compareSync(password, customer.password)) {
currentCustomer = customer;
const token = jwt.sign(
{ username: currentCustomer.username },
process.env.SECRET
);
currentCustomer.token = token;
return {
customer: currentCustomer,
token,
};
} else {
throw new Error("Incorrect password.");
}
},
async checkOut(
parent,
{ id },
{ pets, customers, checkouts, currentCustomer }
) {
if (!currentCustomer) {
throw new Error(
"You have to be logged in to check out a pet."
);
}
let pet = await checkouts.findOne({ petId: id });
let petExists = await pets.findOne({ id });
if (pet) {
throw new Error(
"Sorry, this pet is already checked out."
);
} else if (petExists) {
let checkout = {
petId: id,
username: currentCustomer.username,
checkOutDate: new Date().toISOString(),
};
await checkouts.replaceOne(checkout, checkout, {
upsert: true,
});
return {
customer: await customers.findOne({
username: currentCustomer.username,
}),
pet: await pets.findOne({ id }),
checkOutDate: checkout.checkOutDate,
};
} else {
throw new Error("This pet does not exist.");
}
},
async checkIn(
parent,
{ id },
{ pets, customers, checkouts, currentCustomer }
) {
if (!currentCustomer) {
throw new Error(
"You have to be logged in to check in a pet."
);
}
let pet = await checkouts.findOne({ petId: id });
if (!pet) {
throw new Error("This pet is not checked out.");
} else {
let checkout = await checkouts.findOne({ petId: id });
delete checkout._id;
checkout.checkInDate = new Date().toISOString();
await checkouts.deleteOne({ petId: id });
await customers.updateOne(
{ username: currentCustomer.username },
{
$set: {
checkoutHistory: [
checkout,
...currentCustomer.checkoutHistory,
],
},
}
);
return checkout;
}
},
};