Icu 5476 refactor target model serializer and mirage (#1235)

chore: 🤖 refactor target model to support credentials
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2022-08-08 09:21:07 -07:00
1 parent 2cda4f639e
commit 1f0baceb5e
12 files changed
+210 -66

No files matched your search

+14 -2
View File
@@ -464,19 +464,31 @@ export default function () {
// If adding credential sources, push them into the array
if (method === 'add-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
attrs.applicationCredentialSourceIds.forEach((id) => {
if (!updatedAttrs.credentialLibraryIds.includes(id)) {
updatedAttrs.credentialLibraryIds.push(id);
if (
!updatedAttrs.credentialLibraryIds.includes(id) ||
!updatedAttrs.credentialIds.includes(id)
) {
if (id.startsWith('cred')) {
updatedAttrs.credentialIds.push(id);
} else {
updatedAttrs.credentialLibraryIds.push(id);
}
}
});
}
// If deleting credential sources, filter them out of the array
if (method === 'remove-credential-sources') {
updatedAttrs.credentialLibraryIds = target.credentialLibraryIds;
updatedAttrs.credentialIds = target.credentialIds;
updatedAttrs.credentialLibraryIds =
updatedAttrs.credentialLibraryIds.filter((id) => {
return !attrs.applicationCredentialSourceIds.includes(id);
});
updatedAttrs.credentialIds = updatedAttrs.credentialIds.filter((id) => {
return !attrs.applicationCredentialSourceIds.includes(id);
});
}
return target.update(updatedAttrs);
}
+7 -2
View File
@@ -49,7 +49,9 @@ export default factory.extend({
*/
withAssociations: trait({
afterCreate(target, server) {
let randomlySelectedHostSets, randomlySelectedCredentialLibraries;
let randomlySelectedHostSets,
randomlySelectedCredentialLibraries,
randomlySelectedCredentials;
randomlySelectedHostSets = server.schema.hostSets
// BLERG: fun fact, for no reason at all, the element passed
// into a where function is not a full model instance, as you might
@@ -64,10 +66,13 @@ export default factory.extend({
(credentialLibrary) => credentialLibrary.scopeId === target.scope.id
)
.models.filter(() => randomBoolean());
randomlySelectedCredentials = server.schema.credentials
.where((credential) => credential.scopeId === target.scope.id)
.models.filter(() => randomBoolean());
target.update({
hostSets: randomlySelectedHostSets,
credentialLibraries: randomlySelectedCredentialLibraries,
credentials: randomlySelectedCredentials,
});
},
}),
+1
View File
@@ -5,4 +5,5 @@ export default Model.extend({
scope: belongsTo(),
hostSets: hasMany(),
credentialLibraries: hasMany(),
credentials: hasMany(),
});
+5 -2
View File
@@ -23,8 +23,11 @@ export default ApplicationSerializer.extend({
return { id: host_set_id, host_catalog_id };
});
}
if (model.credentialLibraryIds?.length) {
json.application_credential_source_ids = model.credentialLibraryIds;
if (model.credentialLibraryIds?.length || model.credentialIds?.length) {
json.application_credential_source_ids = [
...model.credentialLibraryIds,
...model.credentialIds,
];
}
return json;
},