api
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (pull_request) Failing after 15s
Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (pull_request) Failing after 15s
This commit is contained in:
82
src/index.ts
82
src/index.ts
@@ -1,6 +1,7 @@
|
||||
import { Hono } from 'hono';
|
||||
import { cors } from 'hono/cors';
|
||||
import { getCookie } from 'hono/cookie';
|
||||
import { Context } from 'hono';
|
||||
import { decryptAuthCookie } from './services/decrypt-service';
|
||||
import { handleSwaggerRequest } from './swagger-ui';
|
||||
import { authMiddleware } from './middleware/auth';
|
||||
@@ -20,7 +21,7 @@ app.use('*', cors());
|
||||
|
||||
// Create a group for protected routes with auth middleware
|
||||
// Exclude Swagger docs routes from auth middleware
|
||||
app.use('/api/cf-template/*', async (c, next) => {
|
||||
app.use('/api/leave-agent/*', async (c, next) => {
|
||||
const path = new URL(c.req.url).pathname;
|
||||
|
||||
// Skip auth for Swagger docs routes
|
||||
@@ -33,7 +34,7 @@ app.use('/api/cf-template/*', async (c, next) => {
|
||||
});
|
||||
|
||||
// Add auth validation endpoint (GET method)
|
||||
app.get('/api/cf-template/auth/validate', async (c) => {
|
||||
app.get('/api/leave-agent/auth/validate', async (c) => {
|
||||
try {
|
||||
// Get auth from query parameter
|
||||
const authToken = c.req.query('auth');
|
||||
@@ -58,7 +59,7 @@ app.get('/api/cf-template/auth/validate', async (c) => {
|
||||
});
|
||||
|
||||
// Add auth validation endpoint (POST method)
|
||||
app.post('/api/cf-template/auth/validate', async (c) => {
|
||||
app.post('/api/leave-agent/auth/validate', async (c) => {
|
||||
try {
|
||||
// Get auth from request body
|
||||
const body = await c.req.json();
|
||||
@@ -83,8 +84,71 @@ app.post('/api/cf-template/auth/validate', async (c) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Define types for the user data
|
||||
interface UserData {
|
||||
id: number;
|
||||
uid: string;
|
||||
email: string;
|
||||
firstname?: string;
|
||||
lastname?: string;
|
||||
company_name?: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
// Define types for the context
|
||||
interface UserContext extends Context {
|
||||
get: (key: string) => string | undefined;
|
||||
env: {
|
||||
CF_TEMPLATE_DB?: any; // Replace 'any' with proper D1Database type if available
|
||||
};
|
||||
}
|
||||
|
||||
// Get current user's profile with leave balances
|
||||
app.get('/api/leave-agent/employees/me', async (c: UserContext) => {
|
||||
try {
|
||||
// Get user ID from the auth middleware
|
||||
const userId = c.get('userId');
|
||||
|
||||
if (!userId) {
|
||||
return c.json({ error: 'User not authenticated' }, 401);
|
||||
}
|
||||
|
||||
// Get user data from the database
|
||||
const userData = await getUserByUid(userId, c.env);
|
||||
|
||||
if (!userData) {
|
||||
return c.json({ error: 'User not found' }, 404);
|
||||
}
|
||||
|
||||
// TODO: Replace with actual leave balance calculation from your database
|
||||
// This is a mock implementation
|
||||
const leaveBalances = {
|
||||
"Annual Leave": 24,
|
||||
"Maternity Leave": 182,
|
||||
"Paternity Leave": 0,
|
||||
"Bereavement Leave": 5,
|
||||
"Marriage Leave": 5
|
||||
};
|
||||
|
||||
// Format the response
|
||||
const response = {
|
||||
id: userData.id,
|
||||
name: `${userData.firstname || ''} ${userData.lastname || ''}`.trim(),
|
||||
email: userData.email,
|
||||
color: '#3b82f6', // Default color, can be customized per user
|
||||
leaveBalances: leaveBalances
|
||||
};
|
||||
|
||||
return c.json(response);
|
||||
} catch (error) {
|
||||
console.error('Error fetching user profile:', error);
|
||||
return c.json({ error: 'Failed to fetch user profile' }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Add endpoint to decrypt and save cookie data to D1 database
|
||||
app.post('/api/cf-template/auth/decrypt-and-save', async (c) => {
|
||||
app.post('/api/leave-agent/auth/decrypt-and-save', async (c) => {
|
||||
try {
|
||||
// Get auth token from request body
|
||||
const body = await c.req.json();
|
||||
@@ -125,15 +189,15 @@ app.post('/api/cf-template/auth/decrypt-and-save', async (c) => {
|
||||
});
|
||||
|
||||
// Add health check endpoint
|
||||
app.get('/api/cf-template/health', (c) => {
|
||||
app.get('/api/leave-agent/health', (c) => {
|
||||
return c.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
// Add Swagger UI routes
|
||||
app.get('/api/cf-template/docs', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
|
||||
app.get('/api/cf-template/docs/', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
|
||||
app.get('/api/cf-template/swagger.json', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
|
||||
app.get('/api/cf-template/openapi.json', (c) => handleSwaggerRequest(c.req.raw, '/api/cf-template'));
|
||||
app.get('/api/leave-agent/docs', (c) => handleSwaggerRequest(c.req.raw, '/api/leave-agent'));
|
||||
app.get('/api/leave-agent/docs/', (c) => handleSwaggerRequest(c.req.raw, '/api/leave-agent'));
|
||||
app.get('/api/leave-agent/swagger.json', (c) => handleSwaggerRequest(c.req.raw, '/api/leave-agent'));
|
||||
app.get('/api/leave-agent/openapi.json', (c) => handleSwaggerRequest(c.req.raw, '/api/leave-agent'));
|
||||
|
||||
// Export the app
|
||||
export default {
|
||||
|
||||
Reference in New Issue
Block a user