fix(projects): #909 СОЗДАТЬ кнопка — apiClient + interim A regions
Root causes: 1. Default axios без withXSRFToken не отправлял CSRF header → 419 silent fail (catch ловил только 422). 2. PDD regions UI (commits 4f60add..f982046) использовал 32-bit маску, несовместимую с schema's 8-битным CHECK chk_projects_region_mask_range → 500 silent fail. Changes (NewProjectDialog.vue): - Replace default axios import с apiClient + ensureCsrfCookie + extractErrorMessage из api/client.ts (same pattern как NewDealDialog). - await ensureCsrfCookie() перед mutating; apiClient.post/patch. - Remove regions <v-autocomplete> + selectedRegions ref + inverted region_mode watcher (interim A — proper 89-codes реализация в Plan 6). - Add general error banner для non-422 ошибок (419/401/500/network). - form.region_mask=255 + region_mode='include' (schema default = вся РФ). Changes (EditProjectDialog.spec.ts): - Switch mock с default axios на apiClient (cascading from above). Verified: Pest 742/739/3sk/0, Vitest 758/3sk/0, vue-tsc 0. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -76,16 +76,17 @@
|
||||
:error-messages="errors.daily_limit_target"
|
||||
/>
|
||||
|
||||
<v-autocomplete
|
||||
v-model="selectedRegions"
|
||||
:items="REGIONS"
|
||||
item-title="name"
|
||||
item-value="code"
|
||||
label="Регионы (пусто = вся РФ)"
|
||||
multiple
|
||||
chips
|
||||
clearable
|
||||
/>
|
||||
<v-alert
|
||||
v-if="generalError"
|
||||
type="error"
|
||||
variant="tonal"
|
||||
density="compact"
|
||||
class="mb-3"
|
||||
closable
|
||||
@click:close="generalError = null"
|
||||
>
|
||||
{{ generalError }}
|
||||
</v-alert>
|
||||
|
||||
<div class="mt-3">
|
||||
<span class="text-caption">Дни недели приёма</span>
|
||||
@@ -112,8 +113,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, watch } from 'vue';
|
||||
import axios from 'axios';
|
||||
import { REGIONS } from '../../constants/regions';
|
||||
import { apiClient, ensureCsrfCookie, extractErrorMessage } from '../../api/client';
|
||||
import type { Project } from '../../stores/projectsStore';
|
||||
import DevIndexBadge from '../../components/DevIndexBadge.vue';
|
||||
|
||||
@@ -124,6 +124,9 @@ const props = defineProps<{
|
||||
}>();
|
||||
const emit = defineEmits(['update:modelValue', 'saved']);
|
||||
|
||||
// region_mask=255 = все 8 ФО (schema default, см. db/schema.sql §projects).
|
||||
// PDD regions UI отключён до закрытия Plan 6 — конфликт с 8-битной ФО-маской
|
||||
// в PhonePrefixService.php (1 phone prefix ↔ 1 ФО, не субъект).
|
||||
const form = reactive({
|
||||
name: '',
|
||||
signal_type: 'site' as 'site' | 'call' | 'sms',
|
||||
@@ -131,26 +134,13 @@ const form = reactive({
|
||||
sms_senders: [] as string[],
|
||||
sms_keyword: '',
|
||||
daily_limit_target: 50,
|
||||
region_mask: 0,
|
||||
region_mask: 255,
|
||||
region_mode: 'include' as 'include' | 'exclude',
|
||||
delivery_days_mask: 127,
|
||||
});
|
||||
const errors = reactive<Record<string, string[]>>({});
|
||||
const saving = ref(false);
|
||||
|
||||
const selectedRegions = ref<number[]>([]);
|
||||
watch(selectedRegions, (codes) => {
|
||||
if (codes.length === 0) {
|
||||
form.region_mask = 0;
|
||||
form.region_mode = 'include';
|
||||
} else {
|
||||
// 32-bit JS bitwise limit — region codes >31 не помещаются в Int32 mask.
|
||||
// На MVP покрываем 1-31 (см. constants/regions.ts); для >31 нужен bigint
|
||||
// или array-колонка (Plan 6 — schema delta).
|
||||
form.region_mask = codes.reduce((acc, c) => (c <= 31 ? acc | (1 << c) : acc), 0);
|
||||
form.region_mode = 'exclude';
|
||||
}
|
||||
});
|
||||
const generalError = ref<string | null>(null);
|
||||
|
||||
const dayLabels = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
|
||||
const selectedDays = ref<number[]>([0, 1, 2, 3, 4, 5, 6]);
|
||||
@@ -166,10 +156,9 @@ function setWorkdays(preset: 'weekdays' | 'all') {
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(open) => {
|
||||
if (open) generalError.value = null;
|
||||
if (open && props.mode === 'edit' && props.project) {
|
||||
Object.assign(form, props.project);
|
||||
// TODO: разобрать region_mask обратно в codes (Plan 6 ↑).
|
||||
selectedRegions.value = [];
|
||||
const days: number[] = [];
|
||||
for (let i = 0; i < 7; i++) if (form.delivery_days_mask & (1 << i)) days.push(i);
|
||||
selectedDays.value = days;
|
||||
@@ -181,11 +170,10 @@ watch(
|
||||
sms_senders: [],
|
||||
sms_keyword: '',
|
||||
daily_limit_target: 50,
|
||||
region_mask: 0,
|
||||
region_mask: 255,
|
||||
region_mode: 'include',
|
||||
delivery_days_mask: 127,
|
||||
});
|
||||
selectedRegions.value = [];
|
||||
selectedDays.value = [0, 1, 2, 3, 4, 5, 6];
|
||||
}
|
||||
},
|
||||
@@ -193,12 +181,14 @@ watch(
|
||||
|
||||
async function submit() {
|
||||
saving.value = true;
|
||||
generalError.value = null;
|
||||
Object.keys(errors).forEach((k) => delete errors[k]);
|
||||
try {
|
||||
await ensureCsrfCookie();
|
||||
if (props.mode === 'edit' && props.project) {
|
||||
await axios.patch(`/api/projects/${props.project.id}`, { ...form });
|
||||
await apiClient.patch(`/api/projects/${props.project.id}`, { ...form });
|
||||
} else {
|
||||
await axios.post('/api/projects', { ...form });
|
||||
await apiClient.post('/api/projects', { ...form });
|
||||
}
|
||||
emit('saved');
|
||||
close();
|
||||
@@ -206,6 +196,8 @@ async function submit() {
|
||||
const err = e as { response?: { status?: number; data?: { errors?: Record<string, string[]> } } };
|
||||
if (err.response?.status === 422 && err.response.data?.errors) {
|
||||
Object.assign(errors, err.response.data.errors);
|
||||
} else {
|
||||
generalError.value = extractErrorMessage(e);
|
||||
}
|
||||
} finally {
|
||||
saving.value = false;
|
||||
|
||||
@@ -2,10 +2,17 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { mount, flushPromises } from '@vue/test-utils';
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
import { createVuetify } from 'vuetify';
|
||||
import axios from 'axios';
|
||||
|
||||
vi.mock('axios');
|
||||
vi.mock('../../resources/js/api/client', () => ({
|
||||
apiClient: {
|
||||
post: vi.fn().mockResolvedValue({ data: {} }),
|
||||
patch: vi.fn().mockResolvedValue({ data: {} }),
|
||||
},
|
||||
ensureCsrfCookie: vi.fn().mockResolvedValue(undefined),
|
||||
extractErrorMessage: vi.fn(() => 'Произошла ошибка.'),
|
||||
}));
|
||||
|
||||
import { apiClient } from '../../resources/js/api/client';
|
||||
import EditProjectDialog from '../../resources/js/views/projects/EditProjectDialog.vue';
|
||||
|
||||
const sampleProject = {
|
||||
@@ -52,12 +59,11 @@ describe('EditProjectDialog', () => {
|
||||
});
|
||||
|
||||
it('PATCH on submit', async () => {
|
||||
(axios.patch as ReturnType<typeof vi.fn>).mockResolvedValue({ data: { data: { id: 1 } } });
|
||||
const wrapper = factory({ modelValue: true, project: sampleProject });
|
||||
await flushPromises();
|
||||
await wrapper.find('[data-testid="submit-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
expect(axios.patch).toHaveBeenCalled();
|
||||
expect(apiClient.patch).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('signal_type tabs disabled in edit mode', async () => {
|
||||
|
||||
Reference in New Issue
Block a user