Files
threshold-ui/addons/api/addon/serializers/managed-group.js
T
Cameron Perera f35382a4e0 refactor: 💡 update adapter when transforming filter error (#2702)
* refactor: 💡 update adapter when transforming filter error

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580

* style: 💄 add comment about adapter changes for translated field

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580

* style: 💄 move comment to adapter

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580

* style: 💄 update old comment about model fragments

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580

* refactor: 💡 use extractErrors in serializers

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-16580
2025-03-03 09:58:45 -06:00

71 lines
1.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import ApplicationSerializer from './application';
import {
TYPE_AUTH_METHOD_OIDC,
TYPE_AUTH_METHOD_LDAP,
} from 'api/models/auth-method';
export default class ManagedGroupSerializer extends ApplicationSerializer {
// =properties
/**
* @type {boolean}
*/
serializeScopeID = false;
/**
* Delegates to a type-specific serialization, or default.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
switch (snapshot.record.type) {
case TYPE_AUTH_METHOD_OIDC:
return this.serializeOIDC(...arguments);
case TYPE_AUTH_METHOD_LDAP:
default:
return super.serialize(...arguments);
}
}
serializeOIDC() {
const serialized = super.serialize(...arguments);
if (serialized.attributes.filter_string !== undefined) {
serialized.attributes.filter = serialized.attributes.filter_string;
}
delete serialized.attributes.filter_string;
return serialized;
}
normalize(typeClass, hash, ...rest) {
const normalizedHash = structuredClone(hash);
if (normalizedHash?.attributes?.filter) {
normalizedHash.attributes.filter_string =
normalizedHash.attributes.filter;
delete normalizedHash.attributes.filter;
}
return super.normalize(typeClass, normalizedHash, ...rest);
}
/**
* Extracts errors from the payload and transforms specific keys if needed.
* @override
* @return {object}
*/
extractErrors() {
const errors = super.extractErrors(...arguments);
if (errors?.filter) {
errors.filter_string = errors.filter;
delete errors.filter;
}
return errors;
}
}