Skip to content

Commit 697217c

Browse files
author
Ajit Kumar
committed
fix(delete sponsor api)
1 parent e808c7e commit 697217c

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed

client/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ window.onload = async () => {
4848

4949
Router.add('/payments', (params) => loadModule('payments', params));
5050
Router.add('/admin', (params) => loadModule('admin', params));
51+
Router.add('/admin/sponsors', (params) => loadModule('admin/sponsors', params));
5152
Router.add('/add-payment-method/:mode', (params) => loadModule('addPaymentMethod', params));
5253
Router.add('/faqs/:qHash?', (params) => loadModule('FAQs', params));
5354
Router.add('/policy', () => loadModule('privacyPolicy'));

client/pages/admin/sponsors.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import confirm from 'components/dialogs/confirm';
2+
import './sponsors.scss';
3+
import alert from 'components/dialogs/alert';
4+
import { getLoggedInUser } from 'lib/helpers';
5+
6+
export default async function Sponsors() {
7+
try {
8+
const loggedInUser = await getLoggedInUser();
9+
10+
if (loggedInUser.role !== 'admin') {
11+
return <p>You do not have permission to view this page.</p>;
12+
}
13+
} catch (error) {
14+
console.error('Error fetching logged in user:', error);
15+
return <p>Error fetching logged in user</p>;
16+
}
17+
18+
const sponsors = await fetch('/api/sponsors').then((res) => res.json());
19+
return (
20+
<section id='sponsors'>
21+
<h1>Sponsors</h1>
22+
<ul>
23+
{sponsors.map((sponsor) => (
24+
<li className='sponsor-item'>
25+
<div>
26+
<div className='info'>
27+
<strong>{sponsor.name}</strong>
28+
<span>{sponsor.tier}</span>
29+
</div>
30+
<button
31+
type='button'
32+
className='icon delete'
33+
onclick={async (e) => {
34+
const confirmation = await confirm('Warning', `Are you sure you want to delete ${sponsor.name}?`);
35+
if (confirmation) {
36+
try {
37+
await fetch(`/api/sponsors/${sponsor.id}`, { method: 'DELETE' });
38+
e.target.closest('li.sponsor-item').remove();
39+
} catch (error) {
40+
console.error('Error deleting sponsor:', error);
41+
alert('Error', 'Failed to delete sponsor');
42+
}
43+
}
44+
}}
45+
></button>
46+
</div>
47+
</li>
48+
))}
49+
</ul>
50+
</section>
51+
);
52+
}

client/pages/admin/sponsors.scss

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#sponsors {
2+
ul {
3+
list-style-type: none;
4+
padding: 0;
5+
6+
li {
7+
padding: 8px 12px;
8+
border-bottom: 1px solid #eee;
9+
10+
div {
11+
display: flex;
12+
align-items: center;
13+
justify-content: space-between;
14+
15+
.info {
16+
display: flex;
17+
flex-direction: column;
18+
justify-content: flex-start;
19+
align-items: flex-start;
20+
21+
strong {
22+
font-weight: bold;
23+
}
24+
25+
span {
26+
font-size: 0.9em;
27+
color: #666;
28+
}
29+
}
30+
31+
.icon {
32+
margin: 0;
33+
background: none;
34+
border: none;
35+
cursor: pointer;
36+
37+
&.delete {
38+
color: red;
39+
}
40+
}
41+
}
42+
43+
&:hover {
44+
background-color: #333;
45+
}
46+
}
47+
}
48+
}

server/apis/sponsor.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Sponsor = require('../entities/sponsor');
44
const { resolve } = require('node:path');
55
const { existsSync } = require('node:fs');
66
const { google } = require('googleapis');
7+
const { getLoggedInUser, sendNotification } = require('../lib/helpers');
78

89
const router = Router();
910
const androidpublisher = google.androidpublisher('v3');
@@ -12,7 +13,14 @@ const sponsorImagesPath = resolve(__dirname, '../../data/sponsors');
1213
router.get('/', async (req, res) => {
1314
const { page, limit } = req.query;
1415

15-
const rows = await Sponsor.get(Sponsor.safeColumns, [[Sponsor.STATUS, Sponsor.STATE_PURCHASED]], { page, limit });
16+
const rows = await Sponsor.get(
17+
Sponsor.safeColumns,
18+
[
19+
[Sponsor.STATUS, Sponsor.STATE_PURCHASED],
20+
[Sponsor.PUBLIC, 1],
21+
],
22+
{ page, limit },
23+
);
1624

1725
res.send(rows);
1826
});
@@ -66,11 +74,33 @@ router.post('/', async (req, res) => {
6674
[Sponsor.STATUS, purchase.purchaseState],
6775
);
6876

77+
if (email) {
78+
sendNotification(email, name, 'Thank you for sponsoring Acode', `We appreciate your support, ${name}. Thank you for being a valued sponsor!`);
79+
}
80+
6981
res.status(201).json({ message: 'Thank you for becoming a sponsor!' });
7082
} catch (error) {
7183
console.error('Error processing sponsorship:', error);
7284
res.status(403).json({ error: 'Purchase not valid' });
7385
}
7486
});
7587

88+
router.delete('/:id', async (req, res) => {
89+
const loggedInUser = await getLoggedInUser(req);
90+
91+
if (loggedInUser.role !== 'admin') {
92+
return res.status(403).json({ error: 'Forbidden' });
93+
}
94+
95+
const { id } = req.params;
96+
97+
try {
98+
await Sponsor.delete([Sponsor.ID, id]);
99+
res.status(204).send();
100+
} catch (error) {
101+
console.error('Error deleting sponsor:', error);
102+
res.status(500).json({ error: 'Internal Server Error' });
103+
}
104+
});
105+
76106
module.exports = router;

server/entities/sponsor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const table = `create table if not exists sponsor (
1111
package_name text,
1212
website text,
1313
image text,
14-
tier_name text,
15-
tier_id text,
14+
tier text,
1615
public integer default 0,
1716
status integer default 2,
1817
created_at timestamp default current_timestamp,

0 commit comments

Comments
 (0)