215 lines
6.3 KiB
Dart
215 lines
6.3 KiB
Dart
import 'dart:convert';
|
|
import '../constants/api_constants.dart';
|
|
import '../models/invitation_models.dart';
|
|
import 'http_service.dart';
|
|
import 'auth_service.dart';
|
|
|
|
class InvitationsService {
|
|
static Future<Map<String, dynamic>> getAllInvitations() async {
|
|
try {
|
|
final response = await HttpService.get(
|
|
ApiConstants.getAllInvitationsEndpoint,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
final invitationsResponse = InvitationsResponse.fromJson(responseData);
|
|
|
|
if (invitationsResponse.status) {
|
|
return {'success': true, 'data': invitationsResponse.data};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message':
|
|
invitationsResponse.message ?? 'Failed to fetch invitations',
|
|
};
|
|
}
|
|
} else if (response.statusCode == 401 || response.statusCode == 403) {
|
|
await AuthService.handleAuthenticationError();
|
|
return {
|
|
'success': false,
|
|
'message': 'Session expired. Please login again.',
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': 'Server error (${response.statusCode})',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Error fetching invitations: $e');
|
|
return {
|
|
'success': false,
|
|
'message': 'Network error. Please check your connection.',
|
|
};
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> acceptInvitation(int invitationId) async {
|
|
try {
|
|
final response = await HttpService.post(
|
|
ApiConstants.acceptInvitationEndpoint,
|
|
{'id': invitationId},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
return {
|
|
'success': responseData['status'] ?? false,
|
|
'message': responseData['message'] ?? 'Request completed',
|
|
};
|
|
} else if (response.statusCode == 401) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Session expired. Please login again.',
|
|
};
|
|
} else if (response.statusCode == 403) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Access denied. Invalid credentials.',
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': 'Server error (${response.statusCode})',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Error accepting invitation: $e');
|
|
return {
|
|
'success': false,
|
|
'message': 'Network error. Please check your connection.',
|
|
};
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> createInvitation(Map<String, dynamic> invitationData) async {
|
|
try {
|
|
final response = await HttpService.post(
|
|
ApiConstants.createInvitationEndpoint,
|
|
invitationData,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
if (responseData['status'] == true) {
|
|
return {
|
|
'success': true,
|
|
'data': responseData['data'],
|
|
'message': responseData['message'],
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': responseData['message'] ?? 'Failed to create invitation',
|
|
};
|
|
}
|
|
} else if (response.statusCode == 401) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Session expired. Please login again.',
|
|
};
|
|
} else if (response.statusCode == 403) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Access denied. Invalid credentials.',
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': 'Server error (${response.statusCode})',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Error creating invitation: $e');
|
|
return {
|
|
'success': false,
|
|
'message': 'Network error. Please check your connection.',
|
|
};
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> getInvitationDetails(int invitationId) async {
|
|
try {
|
|
final response = await HttpService.get(
|
|
'${ApiConstants.invitationsEndpoint}/get?id=$invitationId',
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
if (responseData['status'] == true) {
|
|
final invitationDetails = InvitationDetails.fromJson(responseData['data']);
|
|
return {
|
|
'success': true,
|
|
'data': invitationDetails,
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': responseData['message'] ?? 'Failed to get invitation details',
|
|
};
|
|
}
|
|
} else if (response.statusCode == 401) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Session expired. Please login again.',
|
|
};
|
|
} else if (response.statusCode == 403) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Access denied. Invalid credentials.',
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': 'Server error (${response.statusCode})',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Error getting invitation details: $e');
|
|
return {
|
|
'success': false,
|
|
'message': 'Network error. Please check your connection.',
|
|
};
|
|
}
|
|
}
|
|
|
|
static Future<Map<String, dynamic>> cancelInvitation(int invitationId) async {
|
|
try {
|
|
final response = await HttpService.post(
|
|
'${ApiConstants.invitationsEndpoint}/cancel',
|
|
{'id': invitationId},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final responseData = jsonDecode(response.body);
|
|
return {
|
|
'success': responseData['status'] ?? false,
|
|
'message': responseData['message'] ?? 'Request completed',
|
|
};
|
|
} else if (response.statusCode == 401) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Session expired. Please login again.',
|
|
};
|
|
} else if (response.statusCode == 403) {
|
|
return {
|
|
'success': false,
|
|
'message': 'Access denied. Invalid credentials.',
|
|
};
|
|
} else {
|
|
return {
|
|
'success': false,
|
|
'message': 'Server error (${response.statusCode})',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Error canceling invitation: $e');
|
|
return {
|
|
'success': false,
|
|
'message': 'Network error. Please check your connection.',
|
|
};
|
|
}
|
|
}
|
|
}
|