feat: 技术支持工单/企微权限/开发版本管理/消息推送等迭代
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import { CatalogService } from './catalog.service';
|
||||
import { OptionalJwtAuthGuard } from '../../common/guards/optional-jwt-auth.guard';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import type { AuthUser } from '../../common/guards/jwt-auth.guard';
|
||||
|
||||
@Controller('catalog')
|
||||
export class CatalogController {
|
||||
constructor(private readonly catalogService: CatalogService) {}
|
||||
|
||||
@Get('cities')
|
||||
cities() {
|
||||
return this.catalogService.listCities();
|
||||
}
|
||||
|
||||
@Get('products')
|
||||
@UseGuards(OptionalJwtAuthGuard)
|
||||
async products(
|
||||
@CurrentUser() user: AuthUser | undefined,
|
||||
@Query('aromaType') aromaType?: string,
|
||||
@Query('cityCode') cityCode?: string,
|
||||
) {
|
||||
const viewerPhone = await this.resolveViewerPhone(user);
|
||||
return this.catalogService.listProducts(aromaType, cityCode, { phone: viewerPhone });
|
||||
}
|
||||
|
||||
@Get('products/:id')
|
||||
@UseGuards(OptionalJwtAuthGuard)
|
||||
async product(@CurrentUser() user: AuthUser | undefined, @Param('id') id: string) {
|
||||
const viewerPhone = await this.resolveViewerPhone(user);
|
||||
return this.catalogService.getProduct(BigInt(id), { phone: viewerPhone });
|
||||
}
|
||||
|
||||
private async resolveViewerPhone(user?: AuthUser) {
|
||||
if (!user || user.actorType !== 'USER') return null;
|
||||
return this.catalogService.resolveUserPhone(user.actorId);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
import type { CommonProductItem, CommonResource } from '@prisma/client';
|
||||
|
||||
export type ProductMediaDto = {
|
||||
mainImageUrl: string | null;
|
||||
carouselUrls: string[];
|
||||
detailImageUrls: string[];
|
||||
};
|
||||
|
||||
type ProductWithCover = CommonProductItem & {
|
||||
coverResource?: { url: string } | null;
|
||||
};
|
||||
|
||||
function urlsFromResources(resources: CommonResource[], bizType: 'CAROUSEL' | 'DETAIL') {
|
||||
return resources
|
||||
.filter((r) => r.bizType === bizType && r.url)
|
||||
.sort((a, b) => a.sortOrder - b.sortOrder)
|
||||
.map((r) => r.url);
|
||||
}
|
||||
|
||||
export function mapProductMedia(
|
||||
product: ProductWithCover,
|
||||
extraResources: CommonResource[] = [],
|
||||
): ProductMediaDto {
|
||||
const mainImageUrl = product.coverResource?.url ?? null;
|
||||
const carouselFromDb = urlsFromResources(extraResources, 'CAROUSEL');
|
||||
const detailFromDb = urlsFromResources(extraResources, 'DETAIL');
|
||||
|
||||
const detailFromJson = parseDetailContentImages(product.detailContent);
|
||||
|
||||
const carouselUrls =
|
||||
carouselFromDb.length > 0
|
||||
? carouselFromDb
|
||||
: mainImageUrl
|
||||
? [mainImageUrl]
|
||||
: [];
|
||||
|
||||
const detailImageUrls =
|
||||
detailFromDb.length > 0
|
||||
? detailFromDb
|
||||
: detailFromJson;
|
||||
|
||||
return { mainImageUrl, carouselUrls, detailImageUrls };
|
||||
}
|
||||
|
||||
function parseDetailContentImages(detailContent: unknown): string[] {
|
||||
if (!detailContent || typeof detailContent !== 'object') return [];
|
||||
const record = detailContent as Record<string, unknown>;
|
||||
const images = record.images ?? record.detailImages ?? record.detailImageUrls;
|
||||
if (!Array.isArray(images)) return [];
|
||||
return images.filter((item): item is string => typeof item === 'string' && item.length > 0);
|
||||
}
|
||||
|
||||
export function groupResourcesByProductId(resources: CommonResource[]) {
|
||||
const map = new Map<string, CommonResource[]>();
|
||||
for (const resource of resources) {
|
||||
const key = resource.ownerId.toString();
|
||||
const list = map.get(key) ?? [];
|
||||
list.push(resource);
|
||||
map.set(key, list);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { IamModule } from '../iam/iam.module';
|
||||
import { CatalogController } from './catalog.controller';
|
||||
import { CatalogService } from './catalog.service';
|
||||
|
||||
@Module({
|
||||
imports: [IamModule],
|
||||
controllers: [CatalogController],
|
||||
providers: [CatalogService],
|
||||
exports: [CatalogService],
|
||||
})
|
||||
export class CatalogModule {}
|
||||
@@ -1,165 +0,0 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
import { groupResourcesByProductId, mapProductMedia } from './catalog.mapper';
|
||||
|
||||
export type CatalogViewer = {
|
||||
/** C 端用户手机号;无则无法看到白名单商品 */
|
||||
phone?: string | null;
|
||||
/** 仅总部代下单等运营场景跳过白名单;合伙人端必须遵守白名单 */
|
||||
bypassWhitelist?: boolean;
|
||||
};
|
||||
|
||||
function normalizePhone(phone: string | null | undefined): string {
|
||||
return (phone || '').replace(/\D/g, '').trim();
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class CatalogService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async listCities() {
|
||||
const cities = await this.prisma.commonCity.findMany({
|
||||
where: { status: 'ACTIVE' },
|
||||
include: {
|
||||
partnerAccounts: {
|
||||
where: { isPrimary: 1, bindingStatus: 'ACTIVE' },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
select: { id: true, companyName: true, scopeType: true },
|
||||
},
|
||||
},
|
||||
orderBy: { name: 'asc' },
|
||||
});
|
||||
return serializeBigInt(
|
||||
cities.map((city) => ({
|
||||
...city,
|
||||
partnerBindingCount: city.partnerAccounts.length,
|
||||
partnerBindings: city.partnerAccounts.map((bp) => ({
|
||||
partnerAccountId: bp.id.toString(),
|
||||
partnerId: bp.id.toString(),
|
||||
companyName: bp.companyName,
|
||||
scopeType: bp.scopeType,
|
||||
})),
|
||||
partnerAccounts: undefined,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
async listProducts(aromaType?: string, cityCode?: string, viewer?: CatalogViewer) {
|
||||
if (cityCode) {
|
||||
const city = await this.prisma.commonCity.findFirst({
|
||||
where: { code: cityCode, status: 'ACTIVE' },
|
||||
});
|
||||
if (!city) throw new BadRequestException('该城市暂未开城');
|
||||
}
|
||||
|
||||
const products = await this.prisma.commonProductItem.findMany({
|
||||
where: { status: 'ON_SALE', ...(aromaType ? { aromaType: aromaType as never } : {}) },
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
include: {
|
||||
coverResource: true,
|
||||
visibilityPhones: { select: { phone: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const visible = products.filter((p) => this.isVisibleToViewer(p, viewer));
|
||||
|
||||
const productIds = visible.map((p) => p.id);
|
||||
const resources = productIds.length
|
||||
? await this.prisma.commonResource.findMany({
|
||||
where: {
|
||||
ownerType: 'PRODUCT',
|
||||
ownerId: { in: productIds },
|
||||
status: 'ACTIVE',
|
||||
bizType: { in: ['CAROUSEL', 'DETAIL'] },
|
||||
},
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
})
|
||||
: [];
|
||||
const resourceMap = groupResourcesByProductId(resources);
|
||||
|
||||
return serializeBigInt(
|
||||
visible.map((p) => {
|
||||
const media = mapProductMedia(p, resourceMap.get(p.id.toString()) ?? []);
|
||||
const { visibilityPhones: _phones, visibilityWhitelistEnabled: _wl, ...rest } = p;
|
||||
return {
|
||||
...rest,
|
||||
benefitAmount: p.benefitAmount ?? p.price,
|
||||
price: Number(p.price),
|
||||
benefitDisplay: Number(p.benefitAmount ?? p.price),
|
||||
...media,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async getProduct(id: bigint, viewer?: CatalogViewer) {
|
||||
const product = await this.prisma.commonProductItem.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
coverResource: true,
|
||||
visibilityPhones: { select: { phone: true } },
|
||||
},
|
||||
});
|
||||
if (!product) return null;
|
||||
if (!this.isVisibleToViewer(product, viewer)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const resources = await this.prisma.commonResource.findMany({
|
||||
where: {
|
||||
ownerType: 'PRODUCT',
|
||||
ownerId: id,
|
||||
status: 'ACTIVE',
|
||||
bizType: { in: ['CAROUSEL', 'DETAIL'] },
|
||||
},
|
||||
orderBy: { sortOrder: 'asc' },
|
||||
});
|
||||
|
||||
const media = mapProductMedia(product, resources);
|
||||
const { visibilityPhones: _phones, visibilityWhitelistEnabled: _wl, ...rest } = product;
|
||||
return serializeBigInt({
|
||||
...rest,
|
||||
benefitAmount: product.benefitAmount ?? product.price,
|
||||
price: Number(product.price),
|
||||
...media,
|
||||
});
|
||||
}
|
||||
|
||||
/** 下单前校验:白名单商品仅白名单手机号可买 */
|
||||
async assertPurchasable(productId: bigint, viewerPhone?: string | null) {
|
||||
const product = await this.prisma.commonProductItem.findUnique({
|
||||
where: { id: productId },
|
||||
include: { visibilityPhones: { select: { phone: true } } },
|
||||
});
|
||||
if (!product || product.status !== 'ON_SALE') {
|
||||
throw new BadRequestException('商品不可购买');
|
||||
}
|
||||
if (!this.isVisibleToViewer(product, { phone: viewerPhone })) {
|
||||
throw new BadRequestException('该商品暂不对当前账号开放');
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
async resolveUserPhone(userId: bigint): Promise<string | null> {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { phone: true },
|
||||
});
|
||||
return user?.phone ?? null;
|
||||
}
|
||||
|
||||
isVisibleToViewer(
|
||||
product: {
|
||||
visibilityWhitelistEnabled: boolean;
|
||||
visibilityPhones: Array<{ phone: string }>;
|
||||
},
|
||||
viewer?: CatalogViewer,
|
||||
): boolean {
|
||||
if (viewer?.bypassWhitelist) return true;
|
||||
if (!product.visibilityWhitelistEnabled) return true;
|
||||
const phone = normalizePhone(viewer?.phone);
|
||||
if (!phone) return false;
|
||||
return product.visibilityPhones.some((row) => normalizePhone(row.phone) === phone);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user