- Added package.json with dependencies and scripts for building and testing the API. - Implemented DIContainer for managing service instances (Cosmos DB, Key Vault). - Created createDonation function to handle donation creation and Stripe payment processing. - Implemented getDonations function for fetching donations with pagination and filtering. - Defined types for Donation, Volunteer, Program, and API responses. - Configured TypeScript with tsconfig.json for strict type checking and output settings. - Developed deployment scripts for production and simple deployments to Azure. - Created Bicep templates for infrastructure setup including Cosmos DB, Key Vault, and Function App. - Added parameters for deployment configuration in main.parameters.json. - Configured static web app settings in staticwebapp.config.json for routing and security.
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
|
|
import DIContainer from '../DIContainer';
|
|
import { ApiResponse, PaginatedResponse, Donation } from '../types';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
export async function getDonations(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
|
|
try {
|
|
await DIContainer.getInstance().initializeServices();
|
|
const { donationsContainer } = DIContainer.getInstance().getServices();
|
|
|
|
const page = parseInt(request.query.get('page') || '1');
|
|
const limit = parseInt(request.query.get('limit') || '10');
|
|
const status = request.query.get('status');
|
|
const program = request.query.get('program');
|
|
|
|
let query = 'SELECT * FROM c WHERE 1=1';
|
|
const parameters: any[] = [];
|
|
|
|
if (status) {
|
|
query += ' AND c.status = @status';
|
|
parameters.push({ name: '@status', value: status });
|
|
}
|
|
|
|
if (program) {
|
|
query += ' AND c.program = @program';
|
|
parameters.push({ name: '@program', value: program });
|
|
}
|
|
|
|
query += ' ORDER BY c.createdAt DESC';
|
|
|
|
const { resources: donations } = await donationsContainer.items
|
|
.query({
|
|
query,
|
|
parameters
|
|
})
|
|
.fetchAll();
|
|
|
|
// Simple pagination
|
|
const total = donations.length;
|
|
const pages = Math.ceil(total / limit);
|
|
const startIndex = (page - 1) * limit;
|
|
const endIndex = startIndex + limit;
|
|
const paginatedDonations = donations.slice(startIndex, endIndex);
|
|
|
|
const response: PaginatedResponse<Donation> = {
|
|
success: true,
|
|
data: paginatedDonations,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
pages
|
|
},
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
return {
|
|
status: 200,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
} catch (error) {
|
|
context.error('Error fetching donations:', error);
|
|
|
|
const response: ApiResponse = {
|
|
success: false,
|
|
error: 'Failed to fetch donations',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
return {
|
|
status: 500,
|
|
jsonBody: response,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
app.http('getDonations', {
|
|
methods: ['GET'],
|
|
authLevel: 'anonymous',
|
|
route: 'donations',
|
|
handler: getDonations
|
|
}); |