feat: Android APP V1 - Experimental Alpha
This commit is contained in:
58
server/src/routes/device-tokens.ts
Normal file
58
server/src/routes/device-tokens.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Router } from 'express';
|
||||
import {
|
||||
dispatchPushToUser,
|
||||
listDeviceTokensForUser,
|
||||
upsertDeviceToken
|
||||
} from '../services/push-dispatch.service';
|
||||
|
||||
export interface DeviceTokenRecord {
|
||||
userId: string;
|
||||
platform: 'ios' | 'android';
|
||||
token: string;
|
||||
updatedAt: number;
|
||||
}
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
const { userId, platform, token } = req.body as Partial<DeviceTokenRecord>;
|
||||
|
||||
if (!userId || !token || (platform !== 'ios' && platform !== 'android')) {
|
||||
return res.status(400).json({ error: 'Missing or invalid userId/platform/token' });
|
||||
}
|
||||
|
||||
await upsertDeviceToken({ userId, platform, token });
|
||||
|
||||
res.status(201).json({ ok: true });
|
||||
});
|
||||
|
||||
router.get('/:userId', async (req, res) => {
|
||||
const records = await listDeviceTokensForUser(req.params.userId);
|
||||
|
||||
res.json({
|
||||
tokens: records.map((record) => ({
|
||||
userId: record.userId,
|
||||
platform: record.platform,
|
||||
token: record.token,
|
||||
updatedAt: record.updatedAt
|
||||
}))
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/:userId/dispatch', async (req, res) => {
|
||||
const { title, body, data } = req.body as {
|
||||
title?: string;
|
||||
body?: string;
|
||||
data?: Record<string, string>;
|
||||
};
|
||||
|
||||
if (!title || !body) {
|
||||
return res.status(400).json({ error: 'Missing title/body' });
|
||||
}
|
||||
|
||||
await dispatchPushToUser(req.params.userId, { title, body, data });
|
||||
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user