Skip to content

Commit f84602b

Browse files
author
Ajit Kumar
committed
fix: plugin api
1 parent bc5c393 commit f84602b

9 files changed

Lines changed: 47 additions & 21 deletions

File tree

.prettierrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

biome.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
"vcs": { "enabled": false, "clientKind": "git", "useIgnoreFile": false },
44
"files": {
55
"ignoreUnknown": true,
6-
"includes": ["**", "!**/*.css", "!**/public/**/*", "!**/*.d.ts", "!**/.vscode/**/*", "!**/jsconfig.json", "!**/.babelrc", "!**/.hintrc"]
6+
"includes": [
7+
"**",
8+
"!**/data/*",
9+
"!**/*.css",
10+
"!**/public/**/*",
11+
"!**/*.d.ts",
12+
"!**/.vscode/**/*",
13+
"!**/jsconfig.json",
14+
"!**/.babelrc",
15+
"!**/.hintrc"
16+
]
717
},
818
"formatter": {
919
"enabled": true,

server/apis/comment.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,7 @@ router.post('/', async (req, res) => {
140140
return;
141141
}
142142

143-
await Comment.insert(
144-
[Comment.PLUGIN_ID, pluginId],
145-
[Comment.USER_ID, loggedInUser.id],
146-
[Comment.COMMENT, comment],
147-
[Comment.VOTE, vote],
148-
);
143+
await Comment.insert([Comment.PLUGIN_ID, pluginId], [Comment.USER_ID, loggedInUser.id], [Comment.COMMENT, comment], [Comment.VOTE, vote]);
149144

150145
if (vote !== Comment.VOTE_NULL) {
151146
updateVoteInPlugin(vote, pluginId);

server/apis/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ router.post('/', async (req, res) => {
382382
return;
383383
}
384384

385-
const { name, price, version, minVersionCode } = pluginJson;
385+
const { name, price, version, minVersionCode = -1 } = pluginJson;
386386

387387
if (!VERSION_REGEX.test(version)) {
388388
res.status(400).send({
@@ -392,7 +392,7 @@ router.post('/', async (req, res) => {
392392
}
393393

394394
if (typeof minVersionCode !== 'number') {
395-
res.status(400).send({ error: 'minVersionCode should be a number' });
395+
res.status(400).send({ error: `minVersionCode should be a number but got ${typeof minVersionCode}` });
396396
return;
397397
}
398398

server/apis/user.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ route.post('/payment-method', async (req, res) => {
312312

313313
const count = await PaymentMethod.count([PaymentMethod.USER_ID, loggedInUser.id]);
314314
insert.push([PaymentMethod.USER_ID, loggedInUser.id]);
315-
await PaymentMethod.insert(...insert, [PaymentMethod.IS_DEFAULT, count === 0]);
315+
await PaymentMethod.insert(...insert, [PaymentMethod.IS_DEFAULT, count === 0 ? 1 : 0]);
316316
res.send({ message: 'Payment method added' });
317317
} catch (error) {
318318
handleError(res, error);
@@ -434,15 +434,19 @@ route.put('/', async (req, res) => {
434434
}
435435

436436
if (email) {
437-
const row = await User.get([User.EMAIL, email]);
437+
if (!sentOtp) {
438+
res.status(400).send({ error: 'Missing OTP' });
439+
return;
440+
}
438441

442+
const row = await User.get([User.EMAIL, email]);
439443
if (row.length) {
440444
res.status(400).send({ error: 'User already' });
441445
return;
442446
}
443447

444-
const [{ otp: storedOtp }] = await Otp.get([Otp.EMAIL, email]);
445-
if (storedOtp !== sentOtp) {
448+
const [otpRow] = await Otp.get([Otp.EMAIL, email]);
449+
if (otpRow?.otp !== sentOtp) {
446450
res.status(400).send({ error: 'Invalid OTP' });
447451
return;
448452
}

server/entities/plugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ class Plugin extends Entity {
136136
return this.update([this.STATUS, this.STATUS_INACTIVE], where, operator);
137137
}
138138

139-
deletePermanently(where, operator = 'AND') {
139+
async deletePermanently(where, operator = 'AND') {
140+
const [row] = await this.get(this.allColumns, where, operator);
141+
if (!row) {
142+
throw new Error('Plugin not found');
143+
}
144+
145+
await Entity.execSql('DELETE FROM comment WHERE plugin_id = ?', [row.id]);
146+
await Entity.execSql('DELETE FROM download WHERE plugin_id = ?', [row.id]);
147+
140148
return super.delete(where, operator);
141149
}
142150

server/entities/user.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ class User extends Entity {
5858
}
5959
}
6060

61+
async delete(where, operator = 'AND') {
62+
const [row] = await this.get(this.allColumns, where, operator);
63+
if (!row) {
64+
throw new Error('User not found');
65+
}
66+
67+
return super.delete(where, operator);
68+
}
69+
6170
get columns() {
6271
return [
6372
this.ID,

server/lib/db.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ if (!fs.existsSync(dbFile)) {
1111
fs.writeFileSync(dbFile, '');
1212
}
1313

14-
module.exports = sqlite3(dbFile);
14+
const db = sqlite3(dbFile);
15+
16+
// Enable foreign key constraints
17+
db.pragma('foreign_keys = ON');
18+
19+
module.exports = db;

updateSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ const queries = ['ALTER TABLE sponsor ADD COLUMN tagline TEXT'];
3434
});
3535

3636
process.exit(0);
37-
})();
37+
})();

0 commit comments

Comments
 (0)