Skip to content

Commit

Permalink
fix: add warranty request constraint
Browse files Browse the repository at this point in the history
- add filter guide
- add warranty item info
- add warranty requests constraint
  • Loading branch information
Pear104 committed Jul 13, 2024
1 parent 1e99bf6 commit f73a606
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 107 deletions.
5 changes: 4 additions & 1 deletion backend/DTOs/WarrantyCard/WarrantyCardDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.DTOs.Accessory;

namespace backend.DTOs.WarrantyCard
{
Expand All @@ -13,7 +14,9 @@ public class WarrantyCardDTO
public string? CustomerName { get; set; }
public string? AccessoryName { get; set; }
public string? DiamondName { get; set; }
public AccessoryDTO? Accessory { get; set; }
public DiamondDTO? Diamond { get; set; }
public DateTime StartTime { get; set; } = DateTime.Now;
public DateTime EndTime { get; set; } = DateTime.Now.AddMonths(12);
}
}
}
2 changes: 1 addition & 1 deletion backend/Enums/WarrantyStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public enum WarrantyStatus
Failed,
Completed,
}
}
}
62 changes: 41 additions & 21 deletions backend/Repository/WarrantyCardRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using System.Threading.Tasks;
using backend.Data;
using backend.DTOs.WarrantyCard;
using backend.Enums;
using backend.Helper;
using backend.Interfaces;
using backend.Mappers;
using backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
Expand All @@ -33,23 +35,35 @@ public WarrantyCardRepository(ApplicationDbContext context)
.WarrantyCards.Include(x => x.Diamond)
.ThenInclude(x => x.Shape)
.Include(x => x.Accessory)
.Where(x => x.OrderDetail!.Order.CustomerId == userId)
.Where(x =>
x.OrderDetail!.Order.CustomerId == userId
&& (
// Only get warranty cards that have no request or have warranty requests that are completed
!x.WarrantyRequests.Any()
|| x.WarrantyRequests.Any(wr =>
wr.WarrantyStatus == WarrantyStatus.Completed
)
)
)
.ToListAsync();
return warrantyCardQueries;
}

public async Task<WarrantyCardResult?> getWarrantyCards(WarrantyCardQuery query)
{
var warrantyCardQueries = _context.WarrantyCards.Include(x => x.OrderDetail)
var warrantyCardQueries = _context
.WarrantyCards.Include(x => x.OrderDetail)
.ThenInclude(x => x.Order)
.Include(x => x.Accessory)
.ThenInclude(x => x.AccessoryImages)
.AsQueryable();

// if (query.WarrantyCardId.HasValue)
// {
// warrantyCardQueries = warrantyCardQueries.Where(x =>
// x.WarrantyCardId == query.WarrantyCardId
// );
// }
if (query.WarrantyCardId.HasValue)
{
warrantyCardQueries = warrantyCardQueries.Where(x =>
x.WarrantyCardId == query.WarrantyCardId
);
}
// if (query.DiamondId.HasValue)
// {
// warrantyCardQueries = warrantyCardQueries.Where(x =>
Expand Down Expand Up @@ -80,19 +94,18 @@ public WarrantyCardRepository(ApplicationDbContext context)

if (!query.ProductName.IsNullOrEmpty())
{

warrantyCardQueries = warrantyCardQueries.Where((x) =>
x.Accessory != null ?
(
x.Accessory.Name.Contains(query.ProductName!)
):(
(x.Diamond!.Carat + " Carat, " + x.Diamond.Shape.Name).Contains(query.ProductName!)
)
warrantyCardQueries = warrantyCardQueries.Where(
(x) =>
x.Accessory != null
? (x.Accessory.Name.Contains(query.ProductName!))
: (
(x.Diamond!.Carat + " Carat, " + x.Diamond.Shape.Name).Contains(
query.ProductName!
)
)
);
}



var totalCount = await warrantyCardQueries.CountAsync();

var totalPages = (int)Math.Ceiling(totalCount / (double)query.PageSize);
Expand All @@ -106,11 +119,19 @@ public WarrantyCardRepository(ApplicationDbContext context)
WarrantyCardId = x.WarrantyCardId,
DiamondId = x.DiamondId,
AccessoryId = x.AccessoryId,
Accessory = x.Accessory != null ? x.Accessory.ToAccessoryDTO() : null,
Diamond = x.Diamond != null ? x.Diamond.ToDiamondDTO() : null,
StartTime = x.StartTime,
EndTime = x.EndTime,
CustomerName = x.OrderDetail!.Order.Customer != null ? x.OrderDetail.Order.Customer.Name : null,
CustomerName =
x.OrderDetail!.Order.Customer != null
? x.OrderDetail.Order.Customer.Name
: null,
AccessoryName = x.Accessory != null ? x.Accessory.Name : null,
DiamondName = x.Diamond != null ? x.Diamond.Carat + " Carat, " + x.Diamond.Shape.Name : null
DiamondName =
x.Diamond != null
? x.Diamond.Carat + " Carat, " + x.Diamond.Shape.Name
: null
})
.ToListAsync();

Expand All @@ -123,6 +144,5 @@ public WarrantyCardRepository(ApplicationDbContext context)
TotalCount = totalCount
};
}

}
}
73 changes: 41 additions & 32 deletions frontend/src/components/nav/TopNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useCartStore } from "../../store/cartStore";
import { GET } from "../../utils/request";
import { jwtDecode } from "jwt-decode";
import { getCookie } from "../../utils/cookie";
import { useQueries } from "@tanstack/react-query";

const TopNavItem = ({
children,
Expand Down Expand Up @@ -60,16 +61,24 @@ export default function TopNav() {
const [diamondDrop, setDiamondDrop] = useState(false);
const [menuDrop, setMenuDrop] = useState(false);
const { message } = App.useApp();
const [account, setAccount] = useState<any>();
const location = useLocation();
const navigate = useNavigate();
useEffect(() => {
const fetchData = async () => {
const response = await GET("/api/Accounts/me");
setAccount(response);
};
fetchData();
}, [location, account]);
// useEffect(() => {
// const fetchData = async () => {
// const response = await GET("/api/Accounts/me");
// setAccount(response);
// };
// fetchData();
// }, [location, account]);
const [info] = useQueries({
queries: [
{
queryKey: ["info"],
queryFn: () => GET("/api/Accounts/me"),
staleTime: Infinity,
},
],
});
return (
<div className="top-0 left-0 relative gap-4 py-3 px-4 bg-white w-full shadow-lg h-full">
<div className="flex justify-between md:hidden">
Expand Down Expand Up @@ -102,27 +111,27 @@ export default function TopNav() {
className="cursor-pointer"
onClick={async () => {
const response = await GET("/api/Accounts/me");
console.log(response);
switch(response.role) {
case "Manager":
navigate("/admin");
break;
case "SaleStaff":
navigate("/admin/sale-staffs");
break;
case "DeliveryStaff":
navigate("/admin/delivery-staffs");
break;
case "Administrator":
navigate("/admin");
break;
case "WarrantyStaff":
navigate("/admin/warranty-request");
break;
default:
navigate("/");
break;
}
console.log(response);
switch (response.role) {
case "Manager":
navigate("/admin");
break;
case "SaleStaff":
navigate("/admin/sale-staffs");
break;
case "DeliveryStaff":
navigate("/admin/delivery-staffs");
break;
case "Administrator":
navigate("/admin");
break;
case "WarrantyStaff":
navigate("/admin/warranty-request");
break;
default:
navigate("/");
break;
}
}}
>
<LineChart size={20} strokeWidth={2} absoluteStrokeWidth />
Expand All @@ -139,9 +148,9 @@ export default function TopNav() {
)}
</Link>
</div>
{account && (
{info?.data && (
<div className="text-lg font-semibold text-gray-800 pl-4 py-2">
Welcome, {account?.name}
Welcome, {info?.data?.name}
</div>
)}
<div
Expand Down Expand Up @@ -198,7 +207,7 @@ export default function TopNav() {
Diamond
</Link>
<TopNavItem setMenuDrop={setMenuDrop} href="/blogs">
Blogs
Blog
</TopNavItem>
<TopNavItem setMenuDrop={setMenuDrop} href="/about">
About us
Expand Down
Loading

0 comments on commit f73a606

Please sign in to comment.