/
create_muster.dart
148 lines (139 loc) · 5.24 KB
/
create_muster.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:works_shg_app/data/repositories/muster_roll_repository/muster_roll.dart';
import 'package:works_shg_app/services/urls.dart';
import '../../data/remote_client.dart';
import '../../models/muster_rolls/muster_roll_model.dart';
import '../../utils/global_variables.dart';
part 'create_muster.freezed.dart';
typedef MusterCreateEmitter = Emitter<MusterCreateState>;
class MusterCreateBloc extends Bloc<MusterCreateEvent, MusterCreateState> {
MusterCreateBloc() : super(const MusterCreateState.initial()) {
on<CreateMusterEvent>(_onCreate);
on<UpdateMusterEvent>(_onUpdate);
}
FutureOr<void> _onCreate(
CreateMusterEvent event, MusterCreateEmitter emit) async {
Client client = Client();
try {
emit(const MusterCreateState.loading());
MusterRollsModel musterRollsModel =
await MusterRollRepository(client.init()).createMuster(
url: Urls.musterRollServices.createMuster,
options: Options(extra: {
"userInfo": GlobalVariables.userRequestModel,
"accessToken": GlobalVariables.authToken
}),
body: {
"musterRoll": {
"tenantId": event.tenantId,
"registerId": event.registerId,
"startDate": event.startDate,
"serviceCode": event.serviceCode,
"referenceId": event.referenceId,
"additionalDetails": {
"orgName": event.orgName,
"contractId": event.contractId,
"attendanceRegisterNo": event.registerNo,
"attendanceRegisterName": event.registerName,
"projectName": event.projectName ?? "",
"amount": event.amount ?? 14500,
"projectType": event.projectType ?? '',
"projectDesc": event.projectDesc ?? '',
"locality": event.locality ?? '',
"ward": event.ward ?? '',
"orgId": GlobalVariables
.organisationListModel?.organisations?.first.id,
"projectId": event.projectId ?? '',
"executingAuthority": event.executingAuthority
},
"individualEntries": event.skillsList ?? []
},
"workflow": {"action": "SUBMIT", "comments": "Submit muster roll"}
});
if (musterRollsModel != null) {
emit(MusterCreateState.loaded(musterRollsModel));
} else {
emit(const MusterCreateState.error());
}
} on DioError catch (e) {
emit(const MusterCreateState.error());
}
}
FutureOr<void> _onUpdate(
UpdateMusterEvent event, MusterCreateEmitter emit) async {
Client client = Client();
try {
emit(const MusterCreateState.loading());
MusterRollsModel musterRollsModel =
await MusterRollRepository(client.init()).createMuster(
url: Urls.musterRollServices.updateMuster,
options: Options(extra: {
"userInfo": GlobalVariables.userRequestModel,
"accessToken": GlobalVariables.authToken
}),
body: {
"musterRoll": {
"tenantId": event.tenantId,
"id": event.id,
"additionalDetails": {"computeAttendance": "true"},
"individualEntries": event.skillsList ?? []
},
"workflow": {
"action": event.reSubmitAction ?? "RE-SUBMIT",
"comments": "Muster Roll ReSubmitted",
"assignees": []
}
});
if (musterRollsModel != null) {
emit(MusterCreateState.loaded(musterRollsModel));
} else {
emit(const MusterCreateState.error());
}
} on DioError catch (e) {
emit(const MusterCreateState.error());
}
}
}
@freezed
class MusterCreateEvent with _$MusterCreateEvent {
const factory MusterCreateEvent.create(
{required String tenantId,
required String registerId,
required String contractId,
required String orgName,
required String registerNo,
required String registerName,
required int startDate,
String? serviceCode,
String? referenceId,
String? projectName,
String? projectDesc,
String? locality,
String? projectId,
String? projectType,
String? ward,
int? amount,
String? executingAuthority,
List<Map<String, dynamic>>? skillsList}) = CreateMusterEvent;
const factory MusterCreateEvent.update(
{required String tenantId,
required String id,
required String orgName,
required String contractId,
required String registerNo,
required String registerName,
String? reSubmitAction,
List<Map<String, dynamic>>? skillsList}) = UpdateMusterEvent;
}
@freezed
class MusterCreateState with _$MusterCreateState {
const MusterCreateState._();
const factory MusterCreateState.initial() = _Initial;
const factory MusterCreateState.loading() = _Loading;
const factory MusterCreateState.loaded(MusterRollsModel? musterRollsModel) =
_Loaded;
const factory MusterCreateState.error() = _Error;
}