42 lines
984 B
Dart
42 lines
984 B
Dart
|
|
import 'package:dio/dio.dart';
|
||
|
|
import '../config/api_config.dart';
|
||
|
|
|
||
|
|
class ChatService {
|
||
|
|
final Dio dio;
|
||
|
|
|
||
|
|
ChatService(this.dio);
|
||
|
|
|
||
|
|
/// Start chat / get ticket
|
||
|
|
Future<Map<String, dynamic>> startChat() async {
|
||
|
|
final res = await dio.get('/user/chat/start');
|
||
|
|
return Map<String, dynamic>.from(res.data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Get all messages
|
||
|
|
Future<List<dynamic>> getMessages(int ticketId) async {
|
||
|
|
final res = await dio.get('/user/chat/messages/$ticketId');
|
||
|
|
return res.data['messages'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Send message (text or file)
|
||
|
|
Future<void> sendMessage(
|
||
|
|
int ticketId, {
|
||
|
|
String? message,
|
||
|
|
String? filePath,
|
||
|
|
}) async {
|
||
|
|
final form = FormData();
|
||
|
|
|
||
|
|
if (message != null) form.fields.add(MapEntry('message', message));
|
||
|
|
if (filePath != null) {
|
||
|
|
form.files.add(
|
||
|
|
MapEntry(
|
||
|
|
'file',
|
||
|
|
await MultipartFile.fromFile(filePath),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
await dio.post('/user/chat/send/$ticketId', data: form);
|
||
|
|
}
|
||
|
|
}
|