41 lines
1.3 KiB
JavaScript
41 lines
1.3 KiB
JavaScript
/// <reference path="../pb_data/types.d.ts" />
|
|
|
|
/**
|
|
* POST endpoint to reset the user's counter to 0,
|
|
* additionally sends a notification to the user.
|
|
* Requires authentication.
|
|
*/
|
|
routerAdd('POST', '/counter/reset', (e) => {
|
|
try {
|
|
const authRecord = e.auth
|
|
|
|
// find the user's counter record
|
|
const counterRecord = $app.findFirstRecordByData('counters', 'userId', authRecord.id)
|
|
|
|
if (counterRecord) {
|
|
// reset the counter to 0 and save
|
|
counterRecord.set('count', 0)
|
|
$app.save(counterRecord)
|
|
}
|
|
|
|
// get the user's language and load the locale
|
|
const userRecord = $app.findFirstRecordByData('users', 'id', authRecord.id)
|
|
const language = userRecord?.getString('language') ?? 'en'
|
|
const locale = require(`${__hooks}/locales/${language}.json`)
|
|
const t = locale.resetCounter
|
|
|
|
// add notification
|
|
const collection = $app.findCollectionByNameOrId('notifications')
|
|
const notificationRecord = new Record(collection)
|
|
notificationRecord.set('userId', authRecord.id)
|
|
notificationRecord.set('title', t.title)
|
|
notificationRecord.set('body', t.body)
|
|
$app.save(notificationRecord)
|
|
|
|
return e.noContent(204)
|
|
} catch (error) {
|
|
console.error('Error', error)
|
|
return e.json(500, { message: error.message })
|
|
}
|
|
}, $apis.requireAuth())
|