Merge branch 'main' of https://gittea.biveki.ru/Sfera/sfera
This commit is contained in:
@ -5115,6 +5115,59 @@ export const resolvers = {
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
updateExternalAdClicks: async (
|
||||
_: unknown,
|
||||
{ id, clicks }: { id: string; clicks: number },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
// Проверяем, что реклама принадлежит организации пользователя
|
||||
const existingAd = await prisma.externalAd.findFirst({
|
||||
where: {
|
||||
id,
|
||||
organizationId: user.organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingAd) {
|
||||
throw new GraphQLError("Внешняя реклама не найдена");
|
||||
}
|
||||
|
||||
await prisma.externalAd.update({
|
||||
where: { id },
|
||||
data: { clicks },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Клики успешно обновлены",
|
||||
externalAd: null,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error updating external ad clicks:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка обновления кликов",
|
||||
externalAd: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// Резолверы типов
|
||||
@ -6117,14 +6170,394 @@ const wildberriesQueries = {
|
||||
},
|
||||
};
|
||||
|
||||
// Резолверы для внешней рекламы
|
||||
const externalAdQueries = {
|
||||
getExternalAds: async (
|
||||
_: unknown,
|
||||
{ dateFrom, dateTo }: { dateFrom: string; dateTo: string },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
const externalAds = await prisma.externalAd.findMany({
|
||||
where: {
|
||||
organizationId: user.organization.id,
|
||||
date: {
|
||||
gte: new Date(dateFrom),
|
||||
lte: new Date(dateTo + 'T23:59:59.999Z'),
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
date: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: null,
|
||||
externalAds: externalAds.map(ad => ({
|
||||
...ad,
|
||||
cost: parseFloat(ad.cost.toString()),
|
||||
date: ad.date.toISOString().split('T')[0],
|
||||
createdAt: ad.createdAt.toISOString(),
|
||||
updatedAt: ad.updatedAt.toISOString(),
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching external ads:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка получения внешней рекламы",
|
||||
externalAds: [],
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const externalAdMutations = {
|
||||
createExternalAd: async (
|
||||
_: unknown,
|
||||
{ input }: { input: { name: string; url: string; cost: number; date: string; nmId: string } },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
const externalAd = await prisma.externalAd.create({
|
||||
data: {
|
||||
name: input.name,
|
||||
url: input.url,
|
||||
cost: input.cost,
|
||||
date: new Date(input.date),
|
||||
nmId: input.nmId,
|
||||
organizationId: user.organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Внешняя реклама успешно создана",
|
||||
externalAd: {
|
||||
...externalAd,
|
||||
cost: parseFloat(externalAd.cost.toString()),
|
||||
date: externalAd.date.toISOString().split('T')[0],
|
||||
createdAt: externalAd.createdAt.toISOString(),
|
||||
updatedAt: externalAd.updatedAt.toISOString(),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating external ad:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка создания внешней рекламы",
|
||||
externalAd: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
updateExternalAd: async (
|
||||
_: unknown,
|
||||
{ id, input }: { id: string; input: { name: string; url: string; cost: number; date: string; nmId: string } },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
// Проверяем, что реклама принадлежит организации пользователя
|
||||
const existingAd = await prisma.externalAd.findFirst({
|
||||
where: {
|
||||
id,
|
||||
organizationId: user.organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingAd) {
|
||||
throw new GraphQLError("Внешняя реклама не найдена");
|
||||
}
|
||||
|
||||
const externalAd = await prisma.externalAd.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: input.name,
|
||||
url: input.url,
|
||||
cost: input.cost,
|
||||
date: new Date(input.date),
|
||||
nmId: input.nmId,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Внешняя реклама успешно обновлена",
|
||||
externalAd: {
|
||||
...externalAd,
|
||||
cost: parseFloat(externalAd.cost.toString()),
|
||||
date: externalAd.date.toISOString().split('T')[0],
|
||||
createdAt: externalAd.createdAt.toISOString(),
|
||||
updatedAt: externalAd.updatedAt.toISOString(),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error updating external ad:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка обновления внешней рекламы",
|
||||
externalAd: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
deleteExternalAd: async (
|
||||
_: unknown,
|
||||
{ id }: { id: string },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
// Проверяем, что реклама принадлежит организации пользователя
|
||||
const existingAd = await prisma.externalAd.findFirst({
|
||||
where: {
|
||||
id,
|
||||
organizationId: user.organization.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingAd) {
|
||||
throw new GraphQLError("Внешняя реклама не найдена");
|
||||
}
|
||||
|
||||
await prisma.externalAd.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Внешняя реклама успешно удалена",
|
||||
externalAd: null,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error deleting external ad:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка удаления внешней рекламы",
|
||||
externalAd: null,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
// Резолверы для кеша склада WB
|
||||
const wbWarehouseCacheQueries = {
|
||||
getWBWarehouseData: async (
|
||||
_: unknown,
|
||||
__: unknown,
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
// Получаем текущую дату без времени
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
// Ищем кеш за сегодня
|
||||
const cache = await prisma.wBWarehouseCache.findFirst({
|
||||
where: {
|
||||
organizationId: user.organization.id,
|
||||
cacheDate: today,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
if (cache) {
|
||||
// Возвращаем данные из кеша
|
||||
return {
|
||||
success: true,
|
||||
message: "Данные получены из кеша",
|
||||
cache: {
|
||||
...cache,
|
||||
cacheDate: cache.cacheDate.toISOString().split('T')[0],
|
||||
createdAt: cache.createdAt.toISOString(),
|
||||
updatedAt: cache.updatedAt.toISOString(),
|
||||
},
|
||||
fromCache: true,
|
||||
};
|
||||
} else {
|
||||
// Кеша нет, нужно загрузить данные из API
|
||||
return {
|
||||
success: true,
|
||||
message: "Кеш не найден, требуется загрузка из API",
|
||||
cache: null,
|
||||
fromCache: false,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error getting WB warehouse cache:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка получения кеша склада WB",
|
||||
cache: null,
|
||||
fromCache: false,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const wbWarehouseCacheMutations = {
|
||||
saveWBWarehouseCache: async (
|
||||
_: unknown,
|
||||
{ input }: { input: { data: string; totalProducts: number; totalStocks: number; totalReserved: number } },
|
||||
context: Context
|
||||
) => {
|
||||
if (!context.user) {
|
||||
throw new GraphQLError("Требуется авторизация", {
|
||||
extensions: { code: "UNAUTHENTICATED" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: context.user.id },
|
||||
include: { organization: true },
|
||||
});
|
||||
|
||||
if (!user?.organization) {
|
||||
throw new GraphQLError("Организация не найдена");
|
||||
}
|
||||
|
||||
// Получаем текущую дату без времени
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
// Используем upsert для создания или обновления кеша
|
||||
const cache = await prisma.wBWarehouseCache.upsert({
|
||||
where: {
|
||||
organizationId_cacheDate: {
|
||||
organizationId: user.organization.id,
|
||||
cacheDate: today,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
data: input.data,
|
||||
totalProducts: input.totalProducts,
|
||||
totalStocks: input.totalStocks,
|
||||
totalReserved: input.totalReserved,
|
||||
},
|
||||
create: {
|
||||
organizationId: user.organization.id,
|
||||
cacheDate: today,
|
||||
data: input.data,
|
||||
totalProducts: input.totalProducts,
|
||||
totalStocks: input.totalStocks,
|
||||
totalReserved: input.totalReserved,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Кеш склада WB успешно сохранен",
|
||||
cache: {
|
||||
...cache,
|
||||
cacheDate: cache.cacheDate.toISOString().split('T')[0],
|
||||
createdAt: cache.createdAt.toISOString(),
|
||||
updatedAt: cache.updatedAt.toISOString(),
|
||||
},
|
||||
fromCache: false,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error saving WB warehouse cache:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : "Ошибка сохранения кеша склада WB",
|
||||
cache: null,
|
||||
fromCache: false,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Добавляем админ запросы и мутации к основным резолверам
|
||||
resolvers.Query = {
|
||||
...resolvers.Query,
|
||||
...adminQueries,
|
||||
...wildberriesQueries,
|
||||
...externalAdQueries,
|
||||
...wbWarehouseCacheQueries,
|
||||
};
|
||||
|
||||
resolvers.Mutation = {
|
||||
...resolvers.Mutation,
|
||||
...adminMutations,
|
||||
...externalAdMutations,
|
||||
...wbWarehouseCacheMutations,
|
||||
};
|
||||
|
Reference in New Issue
Block a user