Skip to content

Commit 4f69724

Browse files
committed
Bug Fixes
-Fixed issue where website would crash after registering for the first time -Fixed bugs that occur when no plants are available -Fixed bug with uploadAvailability
1 parent b9e0e10 commit 4f69724

File tree

6 files changed

+53
-27
lines changed

6 files changed

+53
-27
lines changed

backend/src/handlers.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,9 @@ def create(cls, *args):
9898
'''Create a new model object'''
9999
# If a dict of values is passed in
100100
if len(args) == 1 and type(args[0]) is dict:
101-
print(f'CREATING OBJECT FROM DICT... {args[0]}')
102101
return cls.create_from_dict(args[0])
103102
# If arguments are passed in to match the model's __init___
104103
else:
105-
print('CREATING OBJECT FROM ARGS...')
106-
print(type(args[0]))
107-
print(args[0])
108104
return cls.create_from_args(*args)
109105

110106
@classmethod
@@ -460,7 +456,6 @@ def get_b64(cls, model, size: str):
460456
# Finally, if no image found, return None
461457
return None
462458

463-
464459
@staticmethod
465460
def from_used_for(used_for: ImageUses):
466461
return db.session.query(Image).filter_by(used_for=used_for.value).all()
@@ -974,6 +969,16 @@ def all_customers():
974969
def from_email(email: str):
975970
return User.query.filter(User.personal_email.any(email_address=email)).first()
976971

972+
@staticmethod
973+
def email_in_use(email: str):
974+
'''Returns True if the email is being used by an active account'''
975+
users = User.query.filter(User.personal_email.any(email_address=email)).all()
976+
print('IN EMAIL IN USE')
977+
print(users)
978+
if users is None:
979+
return True
980+
return any(user.account_status != AccountStatus.DELETED.value for user in users)
981+
977982
@staticmethod
978983
def is_customer(user: User):
979984
print(RoleHandler.all_dicts(user.roles))

backend/src/routes.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from src.api import create_app, db
44
from src.models import AccountStatus, Sku, SkuStatus, ImageUses, PlantTraitOptions, OrderStatus
55
from src.handlers import BusinessHandler, UserHandler, SkuHandler, EmailHandler, OrderHandler, OrderItemHandler
6-
from src.handlers import PhoneHandler, PlantHandler, PlantTraitHandler, ImageHandler, ContactInfoHandler
6+
from src.handlers import PhoneHandler, PlantHandler, PlantTraitHandler, ImageHandler, ContactInfoHandler, RoleHandler
77
from src.messenger import welcome, reset_password
88
from src.auth import generate_token, verify_token
99
from src.config import Config
@@ -126,14 +126,22 @@ def consts():
126126
def register():
127127
(data) = getData('data')
128128
# If email is already registered
129-
if any(UserHandler.from_email(e['email_address']) for e in data['emails']):
129+
if any(UserHandler.email_in_use(e['email_address']) for e in data['emails']):
130130
print('ERROR: Email exists')
131131
return StatusCodes['FAILURE_EMAIL_EXISTS']
132+
# Create user
132133
user = UserHandler.create(data)
134+
# Give the user a customer role
135+
user.roles.append(RoleHandler.get_customer_role())
136+
# Update the user's profile data
133137
UserHandler.set_profile_data(user, data)
138+
# Create a session token
139+
token = generate_token(app, user)
140+
UserHandler.set_token(user, token)
141+
db.session.commit()
134142
return {
135143
**StatusCodes['SUCCESS'],
136-
"token": generate_token(app, user),
144+
"token": token,
137145
"user": UserHandler.to_dict(user)
138146
}
139147

@@ -631,8 +639,10 @@ def update_trait(option: PlantTraitOptions, value: str):
631639
db.session.add(new_trait)
632640
return new_trait
633641
print(f"TODOOOOOOOO {plant_data}")
634-
plant_obj.latin_name = plant_data['latin_name']
635-
plant_obj.common_name = plant_data['common_name']
642+
if (latin := plant_data.get('latin_name', None)):
643+
plant_obj.latin_name = latin
644+
if (common := plant_data.get('common_name', None)):
645+
plant_obj.common_name = common
636646
plant_obj.drought_tolerance = update_trait(PlantTraitOptions.DROUGHT_TOLERANCE, plant_data['drought_tolerance'])
637647
plant_obj.grown_height = update_trait(PlantTraitOptions.GROWN_HEIGHT, plant_data['grown_height'])
638648
plant_obj.grown_spread = update_trait(PlantTraitOptions.GROWN_SPREAD, plant_data['grown_spread'])
@@ -684,6 +694,13 @@ def modify_user():
684694
}
685695
if operation in operation_to_status:
686696
user.account_status = operation_to_status[operation]
697+
if operation == 'DELETE':
698+
for email in user.personal_email:
699+
user.personal_email.remove(email)
700+
db.session.delete(email)
701+
for phone in user.personal_phone:
702+
user.personal_phone.remove(phone)
703+
db.session.delete(phone)
687704
db.session.commit()
688705
return {
689706
**StatusCodes['SUCCESS'],

backend/src/uploadAvailability.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def upload_availability(app, data):
4444
# If SKU not found by code or plant, create from scratch
4545
if sku is None:
4646
print(f'Could not find SKU associated with {curr_sku}. Attempting to create new one')
47-
plant = PlantHandler.create(curr_latin)
47+
plant = PlantHandler.from_latin(curr_latin)
48+
if not plant:
49+
plant = PlantHandler.create(curr_latin)
4850
db.session.add(plant)
4951
db.session.commit()
5052
sku = SkuHandler.create()

frontend/src/pages/admin/AdminInventoryPage/AdminInventoryPage.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function AdminInventoryPage() {
5151
}, [])
5252

5353
useEffect(() => {
54-
let ids = existing.map(p => p.display_id);
54+
let ids = existing?.map(p => p.display_id);
55+
if (!ids) return;
5556
getImages(ids, 'm').then(response => {
5657
setExistingThumbnails(response.images);
5758
}).catch(err => {
@@ -60,7 +61,8 @@ function AdminInventoryPage() {
6061
}, [existing])
6162

6263
useEffect(() => {
63-
let ids = existing.map(p => p.display_id);
64+
let ids = existing?.map(p => p.display_id);
65+
if (!ids) return;
6466
getImages(ids, 'm').then(response => {
6567
setAllThumbnails(response.images);
6668
}).catch(err => {
@@ -203,7 +205,7 @@ function AdminInventoryPage() {
203205
<h2>Sort</h2>
204206
<DropDown options={SORT_OPTIONS} onChange={handleExistingSort} initial_value={SORT_OPTIONS[0]} />
205207
<div className="card-flex">
206-
{existing.map((plant, index) => <PlantCard key={index}
208+
{existing?.map((plant, index) => <PlantCard key={index}
207209
plant={plant}
208210
onEdit={() => setCurrPlant(plant)}
209211
thumbnail={existingThumbnails?.length >= index ? existingThumbnails[index] : null} />)}
@@ -213,7 +215,7 @@ function AdminInventoryPage() {
213215
<h2>Sort</h2>
214216
<DropDown options={PLANT_SORT_OPTIONS} onChange={handleAllSort} initial_value={PLANT_SORT_OPTIONS[0]} />
215217
<div className="card-flex">
216-
{all.map((plant, index) => <PlantCard key={index}
218+
{all?.map((plant, index) => <PlantCard key={index}
217219
plant={plant}
218220
onEdit={() => setCurrPlant(plant)}
219221
thumbnail={allThumbnails?.length >= index ? allThumbnails[index] : null} />)}
@@ -336,13 +338,13 @@ function PlantPopup({
336338
setSize(selectedSku['size']);
337339
setPrice(displayPrice(selectedSku['price']));
338340
setQuantity(selectedSku['availability']);
339-
setLatinName(selectedSku['latin_name']);
340-
setCommonName(selectedSku['common_name']);
341-
setDroughtTolerance(selectedSku['drought_tolerance']);
342-
setGrownHeight(selectedSku['grown_height']);
343-
setGrownSpread(selectedSku['grown_spread'])
344-
setOptimalLight(selectedSku['optimal_light']);
345-
setSaltTolerance(selectedSku['salt_tolerance']);
341+
setLatinName(selectedSku['plant']['latin_name']);
342+
setCommonName(selectedSku['plant']['common_name']);
343+
setDroughtTolerance(selectedSku['plant']['drought_tolerance']?.value);
344+
setGrownHeight(selectedSku['plant']['grown_height']?.value);
345+
setGrownSpread(selectedSku['plant']['grown_spread']?.value)
346+
setOptimalLight(selectedSku['plant']['optimal_light']?.value);
347+
setSaltTolerance(selectedSku['plant']['salt_tolerance']?.value);
346348
}, [selectedSku])
347349

348350
const savePlant = () => {
@@ -447,7 +449,7 @@ function PlantPopup({
447449
<div className="sidenav">
448450
<h3>SKUs</h3>
449451
<div className="sku-list">
450-
{skus.map(s => (
452+
{skus?.map(s => (
451453
<div className={`sku-option ${s === selectedSku ? 'selected' : ''}`}
452454
onClick={() => setSelectedSku(s)}>{s.sku}</div>
453455
))}

frontend/src/pages/shopping/ShoppingList/ShoppingList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function ShoppingList({
4343
const loading = useRef(false);
4444

4545
useEffect(() => {
46-
let ids = plants.map(p => p.display_id);
47-
console.log('IDS ARE', ids, plants);
46+
let ids = plants?.map(p => p.display_id);
47+
if (!ids) return;
4848
getImages(ids, 'm').then(response => {
4949
setThumbnails(response.images);
5050
}).catch(err => {
@@ -224,7 +224,7 @@ function ShoppingList({
224224
return (
225225
<StyledShoppingList id={track_scrolling_id}>
226226
{popup}
227-
{plants.map((item, index) =>
227+
{plants?.map((item, index) =>
228228
<PlantCard key={index}
229229
theme={theme}
230230
cart={cart}

frontend/src/query/http_promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function registerUser(email, data) {
121121
return new Promise(function (resolve, reject) {
122122
http.create_user(data).then(response => {
123123
if (response.ok) {
124-
login({email: email, token: data.token }, data.user);
124+
login({email: email, token: response.token }, response.user);
125125
resolve(data);
126126
} else {
127127
console.log('REGISTER FAIL LOGGING OUT')

0 commit comments

Comments
 (0)