Skip to content

Commit 2415a01

Browse files
committed
feat(iam-user): implement update function for data repo and mappers
1 parent bd209fc commit 2415a01

7 files changed

Lines changed: 81 additions & 56 deletions

File tree

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
export interface UpdateUserCommand {
2-
id: string;
1+
import { IoRestorecommerceUserUserType } from '@console-core/graphql';
32

3+
export interface UpdateUserCommand {
4+
id?: string;
5+
name: string;
46
firstName: string;
57
lastName: string;
6-
78
email: string;
8-
9-
defaultScope?: string;
10-
11-
// roleAssignments: RoleAssignment[];
9+
defaultScope: string;
10+
userType: IoRestorecommerceUserUserType;
1211
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { IoRestorecommerceUserUserType } from '@console-core/graphql';
2+
3+
import { UserUpdateFormValue } from '../models';
4+
5+
import { UpdateUserCommand } from './update-user.command';
6+
7+
export function mapFormToUpdateUserCommand(
8+
form: UserUpdateFormValue
9+
): UpdateUserCommand {
10+
return {
11+
id: form.id,
12+
name: form.username,
13+
firstName: form.firstName,
14+
lastName: form.lastName,
15+
email: form.email,
16+
userType: IoRestorecommerceUserUserType.OrgUser,
17+
defaultScope: form.defaultScope,
18+
};
19+
}

packages/modules/iam-user/src/lib/data/user.repository.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ModeType,
1212
} from '@console-core/graphql';
1313

14-
import { CreateUserCommand } from '../commands';
14+
import { CreateUserCommand, UpdateUserCommand } from '../commands';
1515

1616
@Injectable({ providedIn: 'root' })
1717
export class UserRepository {
@@ -120,4 +120,50 @@ export class UserRepository {
120120
})
121121
);
122122
}
123+
124+
updateUser(command: UpdateUserCommand): Observable<{ id: string }> {
125+
return this.userCreateGQL
126+
.mutate({
127+
input: {
128+
items: [command],
129+
scope: command.defaultScope,
130+
mode: ModeType.Update,
131+
},
132+
})
133+
.pipe(
134+
map((result) => {
135+
const details = result.data?.identity.user.Mutate?.details;
136+
137+
const opStatus = details?.operationStatus;
138+
const item = details?.items?.[0];
139+
140+
if (!opStatus || opStatus.code !== 200) {
141+
throw new Error(
142+
opStatus?.message || 'User update failed (operation error)'
143+
);
144+
}
145+
146+
if (!item || item.status?.code !== 200) {
147+
throw new Error(
148+
`User update failed (item status: ${item?.status})`
149+
);
150+
}
151+
152+
const userId = item.payload?.id;
153+
154+
if (!userId) {
155+
throw new Error('User update succeeded but no ID returned');
156+
}
157+
158+
return { id: userId };
159+
}),
160+
catchError((error) => {
161+
// Normalize GraphQL / network / runtime errors
162+
const message =
163+
error?.message || 'Unexpected error during user update';
164+
165+
return throwError(() => new Error(message));
166+
})
167+
);
168+
}
123169
}

packages/modules/iam-user/src/lib/models/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ export * from './user-status.model';
44
export * from './user-detail.model';
55
export * from './user-detail.mapper';
66
export * from './user-create-form.model';
7+
export * from './user-update-form.model';
78
export * from './organization-list.model';
89
export * from './role-list.model';
9-
export * from './user-form.model';

packages/modules/iam-user/src/lib/models/user-form.model.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface UserUpdateFormValue {
2+
id: string;
3+
firstName: string;
4+
lastName: string;
5+
email: string;
6+
username: string;
7+
defaultScope: string;
8+
}
Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +0,0 @@
1-
import { createAction, props } from '@ngrx/store';
2-
3-
import { UpdateUserCommand } from '../../commands';
4-
import { UserFormValue } from '../../models';
5-
6-
export const loadUser = createAction(
7-
'[IAM User Edit] Load User',
8-
props<{ id: string }>()
9-
);
10-
11-
export const loadUserSuccess = createAction(
12-
'[IAM User Edit] Load User Success',
13-
props<{ user: UserFormValue }>()
14-
);
15-
16-
export const loadUserFailure = createAction(
17-
'[IAM User Edit] Load User Failure',
18-
props<{ error: string }>()
19-
);
20-
21-
export const updateUser = createAction(
22-
'[IAM User Edit] Update User',
23-
props<{ command: UpdateUserCommand }>()
24-
);
25-
26-
export const updateUserSuccess = createAction(
27-
'[IAM User Edit] Update User Success',
28-
props<{ user: UserFormValue }>()
29-
);
30-
31-
export const updateUserFailure = createAction(
32-
'[IAM User Edit] Update User Failure',
33-
props<{ error: string }>()
34-
);

0 commit comments

Comments
 (0)