app/apiHandler/settingsApiSlice.js

/**
 * @file
 * File : userApiSlice.js\
 * It is used by redux as a state management provider\
 * Defines the user profile api endpoints
 *
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @name userApiSlice
 */
import { apiSlice } from './apiSlice'

export const userApiSlice = apiSlice.injectEndpoints({
  /**
   * Injects additional endpoints into the base API slice.
   * Defines endpoints for querying user features and roles.
   *
   * @category ApiSlice
   * @function
   * @author  Pierre-Yves Léglise <contact@axialdata.net>
   * @param {Object} builder - RTK Query endpoints builder.
   * @example
   *  const { data, isLoading, isFetching } = useGetFeaturesQuery()
   * @returns {Object} Object containing user feature and role endpoint definitions.
   */
  endpoints: (builder) => ({
    /**
     * Endpoint for retrieving a list of user features.
     * Sends a GET request to the `/features` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name getFeatures
     * @param {Object} [params] - Additional query parameters for filtering or pagination.
     * @example
     *  const { data, isLoading, isFetching } = useGetFeaturesQuery()
     * @returns {Object} Result of the API request.
     */
    getFeatures: builder.query({
      query: (params) => ({
        url: '/features',
        method: 'get',
        withCredentials: true,
        params: { ...params },
      }),
      keepUnusedDataFor: 0,
    }),
    /**
     * Endpoint for retrieving a list of user roles.
     * Sends a GET request to the `/roles` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name getRoles
     * @param {Object} [params] - Additional query parameters for filtering or pagination.
     * @example
     *  const { data, isLoading, isFetching } = useGetRolesQuery()
     * @returns {Object} Result of the API request.
     */
    getRoles: builder.query({
      query: (params) => ({
        url: '/roles',
        method: 'get',
        withCredentials: true,
        params: { ...params },
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for updating a user feature in a role.
     * Sends a PATCH request to the `/roles/{roleId}/featuresPrivileges/{featuresPrivileges}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name updateFeatureInRole
     * @param {string} role - Role ID.
     * @param {string} featuresPrivileges - Feature ID.
     * @param {Object} data - Data to update.
     * @example
     *  const { data, isLoading, isFetching } = useUpdateFeatureInRoleMutation()
     * @returns {Object} Result of the API request.
     */
    updateFeatureInRole: builder.mutation({
      query: ({ role, featuresPrivileges, data }) => ({
        url: `/roles/${role}/featuresPrivileges/${featuresPrivileges}`,
        method: 'patch',
        withCredentials: true,
        data: { privileges: { ...data } },
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for adding a new role.
     * Sends a PUT request to the `/roles/new` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name addRole
     * @param {Object} data - Data to add.
     * @example
     *  const { data, isLoading, isFetching } = useAddRoleMutation()
     * @returns {Object} Result of the API request.
     */
    addRole: builder.mutation({
      query: (data) => ({
        url: '/roles/new',
        method: 'put',
        data: data,
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for updating a role.
     * Sends a PATCH request to the `/roles/{roleId}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name updateRole
     * @param {string} role - Role ID.
     * @param {Object} data - Data to update.
     * @example
     *  const { data, isLoading, isFetching } = useUpdateRoleMutation()
     * @returns {Object} Result of the API request.
     */
    updateRole: builder.mutation({
      query: ({ role, data }) => ({
        url: `/roles/${role}`,
        method: 'patch',
        data: data,
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for deleting a role.
     * Sends a DELETE request to the `/roles/{roleId}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name deleteRole
     * @param {string} role - Role ID.
     * @example
     *  const { data, isLoading, isFetching } = useDeleteRoleMutation()
     * @returns {Object} Result of the API request.
     */
    deleteRole: builder.mutation({
      query: ({ role }) => ({
        url: `/roles/${role}`,
        method: 'delete',
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for adding a new feature to a role.
     * Sends a PUT request to the `/roles/{roleId}/featuresPrivileges/new` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name addFeatureToRole
     * @param {string} role - Role ID.
     * @param {Object} data - Data to add.
     * @example
     *  const { data, isLoading, isFetching } = useAddFeatureToRoleMutation()
     * @returns {Object} Result of the API request.
     */
    addFeatureToRole: builder.mutation({
      query: ({ role, data }) => ({
        url: `/roles/${role}/featuresPrivileges/new`,
        method: 'put',
        data: data,
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for deleting a feature from a role.
     * Sends a DELETE request to the `/roles/{roleId}/featuresPrivileges/{featureId}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name deleteFeatureFromRole
     * @param {string} role - Role ID.
     * @param {string} featureId - Feature ID.
     * @example
     *  const { data, isLoading, isFetching } = useDeleteFeatureFromRoleMutation()
     * @returns {Object} Result of the API request.
     */
    deleteFeatureFromRole: builder.mutation({
      query: ({ role, featureId }) => ({
        url: `/roles/${role}/featuresPrivileges/${featureId}`,
        method: 'delete',
        withCredentials: true,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for retrieving a list of app settings.
     * Sends a POST request to the `/appsettings` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name listAppSettings
     * @param {Object} requestFromFrontend - Request from frontend.
     * @example
     *  const { data, isLoading, isFetching } = useListAppSettingsQuery()
     * @returns {Object} Result of the API request.
     */
    listAppSettings: builder.query({
      query: ({ requestFromFrontend }) => ({
        url: '/appsettings',
        method: 'post',
        withCredentials: true,
        data: { requestFromFrontend },
        // params: { ...params },
      }),

      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for updating app settings.
     * Sends a PATCH request to the `/appsettings/{appConfigName}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name updateAppSettings
     * @param {string} appConfigName - App config name.
     * @param {Object} data - Data to update.
     * @example
     *  const { data, isLoading, isFetching } = useUpdateAppSettingsMutation()
     * @returns {Object} Result of the API request.
     */
    updateAppSettings: builder.mutation({
      query: ({ appConfigName, data }) => ({
        url: '/appsettings/' + appConfigName,
        method: 'patch',
        withCredentials: true,
        data: data,
        // params: { ...params },
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for uploading a file to app settings.
     * Sends a PATCH request to the `/appsettings/{appConfigName}` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name uploadSettingFile
     * @param {Object} companyData - Company data.
     * @param {string} companyData.appConfigName - App config name.
     * @param {Object} formData - Form data to upload.
     * @example
     *  const { data, isLoading, isFetching } = useUploadSettingFileMutation()
     * @returns {Object} Result of the API request.
     */
    uploadSettingFile: builder.mutation({
      query: ({ companyData, formData }) => ({
        url: `/appsettings/${companyData.appConfigName}`,
        method: 'patch',
        headers: {
          'content-type': 'multipart/form-data',
        },
        data: formData,
      }),
      keepUnusedDataFor: 0,
    }),

    /**
     * Endpoint for retrieving a token for app settings.
     * Sends a POST request to the `/appsettings/startsetup` endpoint.
     *
     * @method
     * @author  Pierre-Yves Léglise <contact@axialdata.net>
     * @name getTokenForSettings
     * @param {Object} data - Data to update.
     * @example
     *  const { data, isLoading, isFetching } = useGetTokenForSettingsQuery()
     * @returns {Object} Result of the API request.
     */
    getTokenForSettings: builder.query({
      query: ({ data }) => ({
        url: '/appsettings/startsetup',
        method: 'post',
        withCredentials: true,
        data: data,
        // params: { ...params },
      }),
      keepUnusedDataFor: 0,
    }),
  }),
})

/**
 * Hooks for interacting with the user feature and role endpoints.
 *
 * @typedef {Object} Hooks
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @property {Function} useGetFeaturesQuery - Hook to trigger the getFeatures query.
 * @property {Function} useGetRolesQuery - Hook to trigger the getRoles query.
 * @property {Function} useUpdateFeatureInRoleMutation - Hook to trigger the updateFeatureInRole mutation.
 * @property {Function} useAddRoleMutation - Hook to trigger the addRole mutation.
 * @property {Function} useDeleteRoleMutation - Hook to trigger the deleteRole mutation.
 * @property {Function} useUpdateRoleMutation - Hook to trigger the updateRole mutation.
 * @property {Function} useAddFeatureToRoleMutation - Hook to trigger the addFeatureToRole mutation.
 * @property {Function} useDeleteFeatureFromRoleMutation - Hook to trigger the deleteFeatureFromRole mutation.
 * @property {Function} useListAppSettingsQuery - Hook to trigger the listAppSettings query.
 * @property {Function} useUpdateAppSettingsMutation - Hook to trigger the updateAppSettings mutation.
 * @property {Function} useUploadSettingFileMutation - Hook to trigger the uploadSettingFile mutation.
 * @property {Function} useGetTokenForSettingsQuery - Hook to trigger the getTokenForSettings query.
 * @example
 *  const { data, isLoading, isFetching } = useGetFeaturesQuery()
 * @returns {Object} Object containing user feature and role endpoint hooks.
 */
export const {
  useGetFeaturesQuery,
  useGetRolesQuery,
  useUpdateFeatureInRoleMutation,
  useAddRoleMutation,
  useDeleteRoleMutation,
  useUpdateRoleMutation,
  useAddFeatureToRoleMutation,
  useDeleteFeatureFromRoleMutation,
  useListAppSettingsQuery,
  useGetTokenForSettingsQuery,
  useUpdateAppSettingsMutation,
  useUploadSettingFileMutation,
} = userApiSlice