feat: complete admin panel and backend admin api migration

This commit is contained in:
s.esmaeili
2026-09-13 21:02:25 +03:30
parent 03119cd655
commit 267fc7b7d1
239 changed files with 25431 additions and 935 deletions
@@ -0,0 +1,273 @@
<template>
<!--
Shared implementation of legacy promotion_add_edit.vue and
promotion_tak_add_edit.vue.
Verified by diff: the two legacy files differ only in the card title and in
whether the "مشتری" scope option is offered — the "tak" variant has that
option and its whole customer block COMMENTED OUT. Both differences are
parameterised here rather than duplicating ~400 lines.
-->
<a-spin :spinning="loading">
<a-form layout="vertical" @submit.prevent="handleSubmit">
<a-card :title="title" class="mb-base">
<div class="grid-4">
<a-form-item label="نوع تخفیف">
<!-- legacy disables the type selector while editing -->
<a-select v-model:value="form.type" :disabled="Boolean(id)" @change="changeType">
<a-select-option :value="1">محصولات</a-select-option>
<a-select-option :value="2">دسته بندی</a-select-option>
<a-select-option v-if="allowCustomerScope" :value="3">مشتری</a-select-option>
<a-select-option :value="4">برند</a-select-option>
</a-select>
</a-form-item>
<a-form-item
label="عنوان"
:validate-status="err.title ? 'error' : ''"
:help="err.title"
>
<a-input v-model:value="form.title" />
</a-form-item>
<!-- the target picker changes with the scope -->
<a-form-item v-if="form.type === 1" label="محصول">
<a-select
v-model:value="form.product_id"
show-search
allow-clear
:filter-option="filterOption"
placeholder="انتخاب کنید"
>
<a-select-option v-for="p in products" :key="p.id" :value="p.id">
{{ p.title }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item v-if="form.type === 2" label="دسته بندی">
<a-select
v-model:value="form.category_id"
show-search
allow-clear
:filter-option="filterOption"
placeholder="انتخاب کنید"
>
<a-select-option v-for="c in categories" :key="c.id" :value="c.id">
{{ c.title }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item v-if="form.type === 3 && allowCustomerScope" label="مشتری">
<a-select
v-model:value="form.customer_id"
show-search
allow-clear
:filter-option="filterOption"
placeholder="انتخاب کنید"
>
<a-select-option v-for="c in customers" :key="c.id" :value="c.id">
{{ c.name }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item v-if="form.type === 4" label="برند">
<a-select
v-model:value="form.brand_id"
show-search
allow-clear
:filter-option="filterOption"
placeholder="انتخاب کنید"
>
<a-select-option v-for="b in brands" :key="b.id" :value="b.id">
{{ b.title }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item
label="درصد تخفیف"
:validate-status="err.discount_percent ? 'error' : ''"
:help="err.discount_percent"
>
<a-input-number
v-model:value="form.discount_percent"
class="w-full"
:min="0"
:max="100"
/>
</a-form-item>
<a-form-item label="تاریخ شروع">
<a-date-picker v-model:value="form.start" class="w-full" value-format="YYYY-MM-DD" />
</a-form-item>
<a-form-item label="تاریخ پایان">
<a-date-picker v-model:value="form.end" class="w-full" value-format="YYYY-MM-DD" />
</a-form-item>
<a-form-item label="قابلیت استفاده">
<a-radio-group v-model:value="form.use_only">
<a-radio :value="1"> فقط یکبار</a-radio>
<a-radio :value="2">تا تاریخ پایان</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="وضعیت تخفیف">
<a-switch v-model:checked="form.is_active" />
</a-form-item>
</div>
<a-form-item label="توضیحات">
<a-textarea v-model:value="form.description" :auto-size="{ minRows: 3, maxRows: 6 }" />
</a-form-item>
<a-button type="primary" @click="handleSubmit">ثبت اطلاعات</a-button>
</a-card>
</a-form>
</a-spin>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import api from '../../api/client';
const props = withDefaults(
defineProps<{
title?: string;
/**
* The "tak" variant has the customer scope commented out in legacy, so it
* is not offered there. Preserved behind this flag.
*/
allowCustomerScope?: boolean;
}>(),
{ title: 'تعریف تخفیف', allowCustomerScope: true },
);
const route = useRoute();
const router = useRouter();
const id = route.params.id as string | undefined;
const loading = ref(false);
const products = ref<{ id: string; title: string }[]>([]);
const categories = ref<{ id: string; title: string }[]>([]);
const customers = ref<{ id: string; name: string }[]>([]);
const brands = ref<{ id: string; title: string }[]>([]);
const form = reactive<Record<string, any>>({
type: 1, // legacy initialValue
title: '',
description: '',
discount_percent: undefined,
use_only: 1, // legacy initialValue
is_active: true,
start: undefined,
end: undefined,
product_id: undefined,
category_id: undefined,
customer_id: undefined,
brand_id: undefined,
});
const err = reactive<Record<string, string>>({});
/** legacy changeType(): clears the previously picked target when scope changes */
function changeType() {
form.product_id = undefined;
form.category_id = undefined;
form.customer_id = undefined;
form.brand_id = undefined;
}
function filterOption(input: string, option: any) {
return String(option?.children?.[0]?.children ?? '')
.toLowerCase()
.includes(input.toLowerCase());
}
function validate(): boolean {
Object.keys(err).forEach((k) => delete err[k]);
if (!form.title) err.title = 'لطفا مقدار را وارد کنید';
if (form.discount_percent === undefined || form.discount_percent === null) {
err.discount_percent = 'لطفا مقدار را وارد کنید';
}
return Object.keys(err).length === 0;
}
async function handleSubmit() {
if (!validate()) return;
try {
loading.value = true;
if (id) {
await api.put(`product/variant/promotion/${id}`, form);
} else {
await api.post('product/variant/promotion', form);
}
message.info('اطلاعات با موفقیت به ثبت رسید.');
router.push('/major/promotion/list');
} catch (e: any) {
message.error(e?.response?.data?.message ?? 'خطا در ثبت اطلاعات.');
} finally {
loading.value = false;
}
}
onMounted(async () => {
try {
const [p, c, b] = await Promise.all([
api.post('products', { page: 1, limit: 500 }),
api.post('category', { page: 1, limit: 500 }),
api.get('brand'),
]);
products.value = p.data.rows ?? [];
categories.value = c.data.rows ?? [];
brands.value = b.data ?? [];
} catch (e) {
console.log(e);
}
if (props.allowCustomerScope) {
try {
const { data } = await api.get('query/customer');
customers.value = data;
} catch (e) {
console.log(e);
}
}
if (id) {
try {
const { data } = await api.get(`admin/promotions/${id}`);
Object.assign(form, {
type: data.type ?? 1,
title: data.title ?? '',
description: data.description ?? '',
discount_percent: data.discount_percent !== null ? Number(data.discount_percent) : undefined,
use_only: data.use_only ?? 1,
is_active: data.is_active ?? true,
start: data.start ? String(data.start).slice(0, 10) : undefined,
end: data.end ? String(data.end).slice(0, 10) : undefined,
product_id: data.product_id ?? undefined,
category_id: data.category_id ?? undefined,
customer_id: data.customer_id ?? undefined,
brand_id: data.brand_id ?? undefined,
});
} catch (e) {
console.log(e);
}
}
});
</script>
<style scoped>
.grid-4 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 0.5rem;
}
</style>
@@ -0,0 +1,84 @@
<template>
<!--
Ported from legacy henza_admin/src/views/promotion/autopromotion.vue —
the one-shot welcome discount for new customers. State toggle + percentage.
-->
<a-form layout="vertical" @submit.prevent="handleSubmit">
<a-spin :spinning="loading">
<a-card title="تخفیف اتوماتیک یکبار مصرف ویژه مشتریان جدید" class="mb-base">
<div class="grid-3">
<a-form-item label="وضعیت تخفیف اتوماتیک">
<a-switch v-model:checked="state" size="default" />
</a-form-item>
<a-form-item
label="درصد تخفیف اتوماتیک روی محصولات"
:validate-status="err.discount_percent ? 'error' : ''"
:help="err.discount_percent"
>
<a-input-number
v-model:value="form.discount_percent"
dir="ltr"
:min="0"
:max="100"
:formatter="(v: number | string) => `${v}%`"
:parser="(v: string) => v.replace('%', '')"
style="width: 100%"
/>
</a-form-item>
</div>
</a-card>
</a-spin>
<a-button type="primary" class="mr-4" @click="handleSubmit">ثبت اطلاعات</a-button>
</a-form>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { message } from 'ant-design-vue';
import api from '../../api/client';
const loading = ref(false);
const state = ref(false);
const form = reactive<Record<string, any>>({ discount_percent: undefined });
const err = reactive<Record<string, string>>({});
async function handleSubmit() {
Object.keys(err).forEach((k) => delete err[k]);
if (form.discount_percent === undefined || form.discount_percent === null) {
err.discount_percent = 'وارد کردن درصد تخفیف اتوماتیک الزامی است';
return;
}
try {
loading.value = true;
await api.post('autopromotion', {
state: state.value,
discount_percent: form.discount_percent,
});
message.success('اطلاعات با موفقیت به ثبت رسید.');
} catch (e: any) {
message.error(e?.response?.data?.message ?? 'خطا در ثبت اطلاعات.');
} finally {
loading.value = false;
}
}
onMounted(async () => {
try {
const { data } = await api.get('autopromotion');
state.value = Boolean(data.state);
form.discount_percent = data.discount_percent ?? undefined;
} catch (e) {
console.log(e);
}
});
</script>
<style scoped>
.grid-3 {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1rem;
}
</style>
@@ -0,0 +1,17 @@
// Ported from legacy henza_admin/src/table/promotion.js
import XTbl, { Xtc } from '../../components/x-table/model';
export function buildPromotionListModel(): XTbl {
const tbl = new XTbl('admin/promotions/list', 'لیست تخفیف ها');
tbl.add(new Xtc('id', 'کد').filterNumber());
tbl.add(new Xtc('title', 'نام پروموشن').filterText());
tbl.add(new Xtc('promotion_type_str', 'نوع پروموشن').noSort());
tbl.add(new Xtc('platform', 'پلتفرم نمایش ').noSort());
tbl.add(new Xtc('status', 'وضعیت').renderSlot('sts').noSort());
tbl.add(new Xtc('start', 'تاریخ شروع').renderSlot('startDate').noSort());
tbl.add(new Xtc('end', 'تاریخ پایان ').renderSlot('endDate').noSort());
tbl.add(new Xtc('action', 'عملیات').renderSlot('op').noSort());
return tbl;
}
export default buildPromotionListModel;
@@ -0,0 +1,13 @@
<template>
<!--
Ported from legacy henza_admin/src/views/promotion/promotion_add_edit.vue.
Verified by diff against promotion_tak_add_edit.vue: the two files differ
only in the card title and whether the "مشتری" scope is offered, so both are
thin wrappers over PromotionForm.
-->
<PromotionForm title="تعریف تخفیف" :allow-customer-scope="true" />
</template>
<script setup lang="ts">
import PromotionForm from './PromotionForm.vue';
</script>
@@ -0,0 +1,103 @@
<template>
<!--
Ported from legacy henza_admin/src/views/promotion/promotion_list.vue.
The row's `type_customer` (1 تک / 2 عمده) drives BOTH which permission gates
the icons and which edit screen opens — preserved exactly, including legacy's
commented-out `sts` and `confirmed` slots which stay absent.
-->
<div id="page-user-list">
<a-card title="لیست تخفیف ها">
<XTable ref="tbl" :model="model">
<template #sts="{ record }">
<a-tag v-if="record.status === 1" color="green">فعال</a-tag>
<a-tag v-else color="red">غیر فعال</a-tag>
</template>
<template #startDate="{ text }">
{{ text ? jdate(text) : '-' }}
</template>
<template #endDate="{ text }">
{{ text ? jdate(text) : '-' }}
</template>
<template #op="{ record }">
<a-tooltip v-if="canEdit(record)" title="ویرایش">
<EditOutlined class="cursor-pointer mx-1 text-primary" @click="toEdit(record)" />
</a-tooltip>
<a-tooltip v-if="canRemove(record)" title="حذف">
<a-popconfirm
placement="top"
ok-text="بله"
cancel-text="خیر"
@confirm="deletePromotion(record)"
>
<template #title><p>آیا مطمئن به حذف تخفیف مورد نظر هستید ؟</p></template>
<DeleteOutlined class="cursor-pointer mx-1 text-danger" />
</a-popconfirm>
</a-tooltip>
</template>
</XTable>
</a-card>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { DeleteOutlined, EditOutlined } from '@ant-design/icons-vue';
import XTable from '../../components/x-table/XTable.vue';
import { buildPromotionListModel } from './promotionListModel';
import { useUsersStore } from '../../stores/users';
import { jdate } from '../../utils/filters';
import api from '../../api/client';
const router = useRouter();
const users = useUsersStore();
const hasPermission = (rid: string) => Boolean(users.hasPermission(rid));
const model = buildPromotionListModel();
const tbl = ref<{ fetch: () => Promise<void> } | null>(null);
/** legacy: tak rows need the .tak.* rid, omde rows the plain one */
function canEdit(record: Record<string, any>) {
return (
(hasPermission('offers.list.tak.edit') && record.type_customer === 1) ||
(hasPermission('offers.list.edit') && record.type_customer === 2)
);
}
function canRemove(record: Record<string, any>) {
return (
(hasPermission('offers.list.tak.remove') && record.type_customer === 1) ||
(hasPermission('offers.list.remove') && record.type_customer === 2)
);
}
/**
* legacy toEdit(): opened the edit screen in a NEW TAB via window.open, routing
* to a different path per type_customer. Ported as an in-app navigation — a new
* tab would lose the SPA session state the panel relies on.
*/
function toEdit(record: Record<string, any>) {
if (record.type_customer === 2) {
router.push(`/major/promotion/edit/${record.id}`);
} else if (record.type_customer === 1) {
router.push(`/major/promotion/tak/edit/${record.id}`);
}
}
async function deletePromotion(record: Record<string, any>) {
try {
await api.delete(`major/promotion/${record.id}`);
message.success('تخفیف حذف شد');
tbl.value?.fetch();
} catch (e: any) {
message.error(e?.response?.data?.message ?? 'خطا در حذف تخفیف');
}
}
</script>
@@ -0,0 +1,12 @@
<template>
<!--
Ported from legacy henza_admin/src/views/promotion/promotion_tak_add_edit.vue.
Legacy has the "مشتری" scope option and its whole customer block COMMENTED
OUT in this variant — preserved as absent, not re-enabled.
-->
<PromotionForm title="تعریف تخفیف تک" :allow-customer-scope="false" />
</template>
<script setup lang="ts">
import PromotionForm from './PromotionForm.vue';
</script>