|
| 1 | +import { MultivariateOption, ProjectFlag, Res } from 'common/types/responses' |
| 2 | +import { Req } from 'common/types/requests' |
| 3 | +import { service } from 'common/service' |
| 4 | + |
| 5 | +export const multivariateOptionService = service.injectEndpoints({ |
| 6 | + endpoints: (builder) => ({ |
| 7 | + createMultivariateOption: builder.mutation< |
| 8 | + Res['multivariateOption'], |
| 9 | + Req['createMultivariateOption'] |
| 10 | + >({ |
| 11 | + query: (query) => ({ |
| 12 | + body: query.body, |
| 13 | + method: 'POST', |
| 14 | + url: `projects/${query.project_id}/features/${query.feature_id}/mv-options/`, |
| 15 | + }), |
| 16 | + }), |
| 17 | + saveMultivariateOptions: builder.mutation< |
| 18 | + Res['saveMultivariateOptions'], |
| 19 | + Req['saveMultivariateOptions'] |
| 20 | + >({ |
| 21 | + // No invalidatesTags: every save chain already ends with a broad |
| 22 | + // invalidateTags(['ProjectFlag', 'FeatureList']) once the downstream |
| 23 | + // feature-state save completes — invalidating here too would refetch |
| 24 | + // every subscribed query twice per save. |
| 25 | + queryFn: async (args, _, _2, baseQuery) => { |
| 26 | + const featureUrl = `projects/${args.project_id}/features/${args.feature_id}/` |
| 27 | + // Diff against the server's current options rather than any client |
| 28 | + // cache, so stale state can never turn an update into a duplicate |
| 29 | + // create. |
| 30 | + const flagRes = await baseQuery({ method: 'GET', url: featureUrl }) |
| 31 | + if (flagRes.error) { |
| 32 | + return { error: flagRes.error } |
| 33 | + } |
| 34 | + const serverOptions = |
| 35 | + (flagRes.data as ProjectFlag)?.multivariate_options || [] |
| 36 | + const errors: Record<number, any> = {} |
| 37 | + // Results are written back by input index — downstream feature |
| 38 | + // state saves map weights to option ids positionally. Requests run |
| 39 | + // sequentially so newly created options get ascending ids in input |
| 40 | + // order, which is the order the UI displays. |
| 41 | + const ordered: MultivariateOption[] = [] |
| 42 | + for (let i = 0; i < args.multivariate_options.length; i++) { |
| 43 | + const v = args.multivariate_options[i] |
| 44 | + let original |
| 45 | + if (v.id) { |
| 46 | + original = serverOptions.find((m) => m.id === v.id) |
| 47 | + } else if (v.key) { |
| 48 | + original = serverOptions.find((m) => !!m.key && m.key === v.key) |
| 49 | + } |
| 50 | + const body = { |
| 51 | + ...v, |
| 52 | + default_percentage_allocation: 0, |
| 53 | + feature: args.feature_id, |
| 54 | + } |
| 55 | + const res = await baseQuery( |
| 56 | + original |
| 57 | + ? { |
| 58 | + body, |
| 59 | + method: 'PUT', |
| 60 | + url: `${featureUrl}mv-options/${original.id}/`, |
| 61 | + } |
| 62 | + : { |
| 63 | + body, |
| 64 | + method: 'POST', |
| 65 | + url: `${featureUrl}mv-options/`, |
| 66 | + }, |
| 67 | + ) |
| 68 | + if (res.error) { |
| 69 | + errors[i] = (res.error as { data?: any })?.data ?? null |
| 70 | + } else { |
| 71 | + ordered[i] = res.data as MultivariateOption |
| 72 | + } |
| 73 | + } |
| 74 | + if (Object.keys(errors).length) { |
| 75 | + return { data: { errors, multivariate_options: ordered } } |
| 76 | + } |
| 77 | + const deleted = serverOptions.filter( |
| 78 | + (m) => !ordered.find((o) => o?.id === m.id), |
| 79 | + ) |
| 80 | + const deleteResults = await Promise.all( |
| 81 | + deleted.map((m) => |
| 82 | + baseQuery({ |
| 83 | + method: 'DELETE', |
| 84 | + url: `${featureUrl}mv-options/${m.id}/`, |
| 85 | + }), |
| 86 | + ), |
| 87 | + ) |
| 88 | + const failedDelete = deleteResults.find((r) => r.error) |
| 89 | + if (failedDelete) { |
| 90 | + return { error: failedDelete.error } |
| 91 | + } |
| 92 | + return { data: { errors: null, multivariate_options: ordered } } |
| 93 | + }, |
| 94 | + }), |
| 95 | + // END OF ENDPOINTS |
| 96 | + }), |
| 97 | +}) |
| 98 | + |
| 99 | +export async function createMultivariateOption( |
| 100 | + store: any, |
| 101 | + data: Req['createMultivariateOption'], |
| 102 | + options?: Parameters< |
| 103 | + typeof multivariateOptionService.endpoints.createMultivariateOption.initiate |
| 104 | + >[1], |
| 105 | +) { |
| 106 | + return store.dispatch( |
| 107 | + multivariateOptionService.endpoints.createMultivariateOption.initiate( |
| 108 | + data, |
| 109 | + options, |
| 110 | + ), |
| 111 | + ) |
| 112 | +} |
| 113 | +export async function saveMultivariateOptions( |
| 114 | + store: any, |
| 115 | + data: Req['saveMultivariateOptions'], |
| 116 | + options?: Parameters< |
| 117 | + typeof multivariateOptionService.endpoints.saveMultivariateOptions.initiate |
| 118 | + >[1], |
| 119 | +) { |
| 120 | + return store.dispatch( |
| 121 | + multivariateOptionService.endpoints.saveMultivariateOptions.initiate( |
| 122 | + data, |
| 123 | + options, |
| 124 | + ), |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +export const { |
| 129 | + useCreateMultivariateOptionMutation, |
| 130 | + useSaveMultivariateOptionsMutation, |
| 131 | + // END OF EXPORTS |
| 132 | +} = multivariateOptionService |
0 commit comments