Skip to content

Commit 438a40f

Browse files
committed
Add bonus points section in identity portal
1 parent bbc2f7a commit 438a40f

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { BattlePassRepository } from '../../../repository/battlepass.repository';
2+
import { NextApiRequest, NextApiResponse } from 'next';
3+
import { container } from 'tsyringe';
4+
5+
export default async function handler(
6+
req: NextApiRequest,
7+
res: NextApiResponse
8+
) {
9+
if (req.method !== 'POST') {
10+
return res.status(401).send({ message: 'Method not supported' });
11+
}
12+
13+
try {
14+
const repo = container.resolve(BattlePassRepository);
15+
16+
const body = req.body;
17+
18+
// TODO: handle error??
19+
await repo.setBonusPointApproved(body.userId, body.bonusPointId);
20+
21+
return res.status(200).json({ message: 'Success' });
22+
} catch (e) {
23+
return res.status(500).json({ message: e.message });
24+
}
25+
}

apps/dashboard/pages/identity-portal/attendee-details/index.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useHibiscusSupabase } from '@hibiscus/hibiscus-supabase-context';
2121
import BattlepassPointsBar from 'apps/dashboard/components/battlepass/battlepass-points-bar';
2222
import { BsExclamationTriangle } from 'react-icons/bs';
2323
import { MdCheckCircleOutline } from 'react-icons/md';
24+
import axios from 'axios';
2425

2526
const COLUMN_WIDTH = 510;
2627
const TEAM_MEMBER_ICONS = [
@@ -41,6 +42,8 @@ export function Index() {
4142
const [state, setState] = useState(State.LOADING);
4243
const [isModalOpen, setModalOpen] = useState(false);
4344
const [battlepassProgress, setBattlepassProgress] = useState(null);
45+
const [bonusPoints, setBonusPoints] = useState(null);
46+
const [hackerBonusPoints, setHackerBonusPoints] = useState(null);
4447
const router = useRouter();
4548
const { supabase } = useHibiscusSupabase();
4649

@@ -140,6 +143,28 @@ export function Index() {
140143
}
141144
}, [router.isReady, router.query]);
142145

146+
useEffect(() => {
147+
const fetchData = async () => {
148+
const res = await supabase.getClient().from('bonus_points').select();
149+
if (res.error) return;
150+
151+
const res_ = await supabase
152+
.getClient()
153+
.from('bonus_points_log')
154+
.select()
155+
.eq('user_id', user.user_id)
156+
.eq('status', 1);
157+
if (res_.error) return;
158+
159+
setBonusPoints(res.data);
160+
setHackerBonusPoints(res_.data.map((d) => d.bonus_points_id));
161+
};
162+
163+
if (user != null) {
164+
fetchData();
165+
}
166+
}, [user]);
167+
143168
const { user: authUser } = useHibiscusUser();
144169
if (authUser == null) {
145170
return <>Loading</>;
@@ -265,6 +290,48 @@ export function Index() {
265290
</ScrollableListBox>
266291
</div>
267292
</div>
293+
294+
<div className="flex flex-row gap-[40px] flex-wrap">
295+
<div className="flex flex-col flex-1 gap-[10px]">
296+
<h3 className="text-xl m-0">Bonus Points</h3>
297+
<div className="flex flex-col gap-[20px] border-solid border-black border-[1px] rounded-[8px] p-[30px] text-sm">
298+
{bonusPoints?.map((bp) => (
299+
<div
300+
key={bp.id}
301+
className="flex flex-row gap-[20px] items-center"
302+
>
303+
<p>{bp.name}</p>
304+
{hackerBonusPoints.includes(bp.id) ? (
305+
<button
306+
disabled={true}
307+
className="bg-theme-redward px-[20px] py-[8px] text-white rounded-[8px] border-black border-[1px] border-solid text-xs"
308+
>
309+
Already given
310+
</button>
311+
) : (
312+
<button
313+
onClick={async () => {
314+
const res = await axios.post(
315+
'/api/battlepass/bonus-points-approve',
316+
{
317+
userId: user.user_id,
318+
bonusPointId: bp.id,
319+
}
320+
);
321+
if (res.status === 200) {
322+
setHackerBonusPoints([...hackerBonusPoints, bp.id]);
323+
}
324+
}}
325+
className="bg-red-300 hover:bg-theme-redward active:bg-red-300 px-[20px] py-[8px] text-white rounded-[8px] border-black border-[1px] border-solid text-xs"
326+
>
327+
Submit
328+
</button>
329+
)}
330+
</div>
331+
))}
332+
</div>
333+
</div>
334+
</div>
268335
</div>
269336

270337
<CheckInBox

apps/dashboard/repository/battlepass.repository.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,53 @@ export class BattlePassRepository {
7272

7373
if (res.error) throw res.error;
7474
}
75+
76+
async setBonusPointApproved(userId: string, bonusPointId: string) {
77+
const exists = await this.client
78+
.from('bonus_points_log')
79+
.select('*')
80+
.eq('user_id', userId)
81+
.eq('bonus_points_id', bonusPointId)
82+
.eq('status', 1);
83+
84+
if (exists.error != null) {
85+
throw exists.error;
86+
}
87+
88+
if (exists.data.length === 0) {
89+
const res = await this.client
90+
.from('bonus_points_log')
91+
.upsert({
92+
user_id: userId,
93+
bonus_points_id: bonusPointId,
94+
status: 1,
95+
})
96+
.select('bonus_points (points)');
97+
98+
if (res.error) throw res.error;
99+
100+
const userLeaderboardMatches = await this.client
101+
.from('leaderboard')
102+
.select()
103+
.eq('user_id', `${userId}`);
104+
105+
if (userLeaderboardMatches.data.length == 0) {
106+
const resLeaderboard = await this.client.from('leaderboard').insert({
107+
user_id: userId,
108+
bonus_points: res.data[0].bonus_points.points,
109+
});
110+
if (resLeaderboard.error) throw resLeaderboard.error;
111+
} else {
112+
const resLeaderboard = await this.client
113+
.from('leaderboard')
114+
.update({
115+
bonus_points:
116+
res.data[0].bonus_points.points +
117+
userLeaderboardMatches.data[0].bonus_points,
118+
})
119+
.eq('user_id', userId);
120+
if (resLeaderboard.error) throw resLeaderboard.error;
121+
}
122+
}
123+
}
75124
}

0 commit comments

Comments
 (0)