|
|
@@ -10,8 +10,10 @@ import { ActivatedRoute, Router, RouterLink } from '@angular/router';
|
|
10
|
10
|
import { AppHeader } from '../../share/header/app-header';
|
|
11
|
11
|
import { AppSideMenu } from '../../share/side-menu/app-side-menu';
|
|
12
|
12
|
import { FamilyService } from '../../services/family-service';
|
|
|
13
|
+import { MarriageRelationService } from '../../services/marriage-relation-service';
|
|
13
|
14
|
import { Danka } from '../../models/danka';
|
|
14
|
15
|
import { Family } from '../../models/family';
|
|
|
16
|
+import { MarriageRelation } from '../../models/marriage-relation';
|
|
15
|
17
|
|
|
16
|
18
|
@Component({
|
|
17
|
19
|
selector: 'app-family-edit',
|
|
|
@@ -27,9 +29,11 @@ export class FamilyEdit {
|
|
27
|
29
|
familyId: string | null = null;
|
|
28
|
30
|
relationMode: string = '';
|
|
29
|
31
|
baseFamilyId: string = '';
|
|
|
32
|
+ marriageErrorMessages: string[] = [];
|
|
30
|
33
|
|
|
31
|
34
|
constructor(
|
|
32
|
35
|
private familyService: FamilyService,
|
|
|
36
|
+ private marriageRelationService: MarriageRelationService,
|
|
33
|
37
|
private route: ActivatedRoute,
|
|
34
|
38
|
private router: Router,
|
|
35
|
39
|
) {
|
|
|
@@ -52,12 +56,14 @@ export class FamilyEdit {
|
|
52
|
56
|
spouseId: this.family.spouseId,
|
|
53
|
57
|
gender: this.family.gender,
|
|
54
|
58
|
});
|
|
|
59
|
+ this.patchMarriageRelationFields(this.family.id);
|
|
55
|
60
|
}
|
|
56
|
61
|
}
|
|
57
|
62
|
|
|
58
|
63
|
if (!this.familyId && this.relationMode === 'spouse') {
|
|
59
|
64
|
this.familyForm.patchValue({
|
|
60
|
65
|
spouseId: this.baseFamilyId,
|
|
|
66
|
+ marriageStatus: 'current',
|
|
61
|
67
|
});
|
|
62
|
68
|
}
|
|
63
|
69
|
|
|
|
@@ -87,6 +93,7 @@ export class FamilyEdit {
|
|
87
|
93
|
fatherId: new FormControl(''),
|
|
88
|
94
|
motherId: new FormControl(''),
|
|
89
|
95
|
spouseId: new FormControl(''),
|
|
|
96
|
+ marriageStatus: new FormControl<MarriageRelation['status']>('current'),
|
|
90
|
97
|
gender: new FormControl('unknown'),
|
|
91
|
98
|
});
|
|
92
|
99
|
|
|
|
@@ -110,14 +117,43 @@ export class FamilyEdit {
|
|
110
|
117
|
return this.families.filter((family) => family.id !== this.familyId);
|
|
111
|
118
|
}
|
|
112
|
119
|
|
|
|
120
|
+ patchMarriageRelationFields(familyId: string): void {
|
|
|
121
|
+ const relations = this.marriageRelationService.getMarriageRelationsByFamilyId(familyId);
|
|
|
122
|
+ const relation =
|
|
|
123
|
+ relations.find((marriageRelation) => marriageRelation.status === 'current') ?? relations[0];
|
|
|
124
|
+
|
|
|
125
|
+ if (!relation) {
|
|
|
126
|
+ return;
|
|
|
127
|
+ }
|
|
|
128
|
+
|
|
|
129
|
+ this.familyForm.patchValue({
|
|
|
130
|
+ spouseId: relation.person1Id === familyId ? relation.person2Id : relation.person1Id,
|
|
|
131
|
+ marriageStatus: relation.status,
|
|
|
132
|
+ });
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ findMarriageRelation(person1Id: string, person2Id: string): MarriageRelation | undefined {
|
|
|
136
|
+ return this.marriageRelationService
|
|
|
137
|
+ .getMarriageRelationsByFamilyId(person1Id)
|
|
|
138
|
+ .find(
|
|
|
139
|
+ (relation) =>
|
|
|
140
|
+ (relation.person1Id === person1Id && relation.person2Id === person2Id) ||
|
|
|
141
|
+ (relation.person1Id === person2Id && relation.person2Id === person1Id),
|
|
|
142
|
+ );
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
113
|
145
|
saveFamily() {
|
|
114
|
146
|
if (this.familyForm.invalid) {
|
|
115
|
147
|
return;
|
|
116
|
148
|
}
|
|
117
|
149
|
|
|
|
150
|
+ this.marriageErrorMessages = [];
|
|
118
|
151
|
const familyId = this.familyId ?? Date.now().toString();
|
|
119
|
152
|
const dankaId = this.dankaId;
|
|
120
|
153
|
const formValue = this.familyForm.value;
|
|
|
154
|
+ const spouseId = formValue.spouseId ?? '';
|
|
|
155
|
+ const marriageStatus = formValue.marriageStatus ?? 'current';
|
|
|
156
|
+ const currentSpouseId = marriageStatus === 'current' ? spouseId : '';
|
|
121
|
157
|
const updatedFamily = {
|
|
122
|
158
|
id: familyId,
|
|
123
|
159
|
dankaId: dankaId,
|
|
|
@@ -128,9 +164,34 @@ export class FamilyEdit {
|
|
128
|
164
|
note: formValue.note?.trim() ?? '',
|
|
129
|
165
|
fatherId: this.familyForm.value.fatherId ?? '',
|
|
130
|
166
|
motherId: this.familyForm.value.motherId ?? '',
|
|
131
|
|
- spouseId: this.familyForm.value.spouseId ?? '',
|
|
|
167
|
+ spouseId: currentSpouseId,
|
|
132
|
168
|
gender: this.familyForm.value.gender ?? 'unknown',
|
|
133
|
169
|
};
|
|
|
170
|
+
|
|
|
171
|
+ if (spouseId) {
|
|
|
172
|
+ const existingRelation = this.findMarriageRelation(familyId, spouseId);
|
|
|
173
|
+ const errors = this.marriageRelationService.saveMarriageRelation({
|
|
|
174
|
+ id: existingRelation?.id ?? Date.now().toString(),
|
|
|
175
|
+ dankaId,
|
|
|
176
|
+ person1Id: familyId,
|
|
|
177
|
+ person2Id: spouseId,
|
|
|
178
|
+ status: marriageStatus,
|
|
|
179
|
+ startDate: existingRelation?.startDate ?? '',
|
|
|
180
|
+ endDate: existingRelation?.endDate ?? '',
|
|
|
181
|
+ note: existingRelation?.note ?? '',
|
|
|
182
|
+ });
|
|
|
183
|
+
|
|
|
184
|
+ if (errors.length > 0) {
|
|
|
185
|
+ this.marriageErrorMessages = errors;
|
|
|
186
|
+ return;
|
|
|
187
|
+ }
|
|
|
188
|
+ } else {
|
|
|
189
|
+ const currentMarriage = this.marriageRelationService.getCurrentMarriageByFamilyId(familyId);
|
|
|
190
|
+ if (currentMarriage) {
|
|
|
191
|
+ this.marriageRelationService.deleteMarriageRelation(currentMarriage.id);
|
|
|
192
|
+ }
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
134
|
195
|
this.familyService.saveFamily(updatedFamily);
|
|
135
|
196
|
this.router.navigate(['/danka-detail', dankaId], { queryParams: { tab: 'family' } });
|
|
136
|
197
|
}
|