fix(mining-wallet): fix province/city creation and add seed on startup

- Use provinceCode directly instead of inferring from cityCode
- Use code as name for province/city records
- Add ts-node to production for seed execution
- Run prisma db seed on container startup

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-14 00:40:49 -08:00
parent e6e44d9a43
commit 7588d18fff
3 changed files with 14 additions and 14 deletions

View File

@ -30,14 +30,15 @@ WORKDIR /app
USER nestjs USER nestjs
COPY --chown=nestjs:nodejs package*.json ./ COPY --chown=nestjs:nodejs package*.json ./
RUN npm ci --only=production && npm cache clean --force COPY --chown=nestjs:nodejs tsconfig*.json ./
RUN npm ci --only=production && npm install ts-node typescript @types/node --save-dev && npm cache clean --force
COPY --chown=nestjs:nodejs prisma ./prisma/ COPY --chown=nestjs:nodejs prisma ./prisma/
RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate
COPY --chown=nestjs:nodejs --from=builder /app/dist ./dist COPY --chown=nestjs:nodejs --from=builder /app/dist ./dist
RUN printf '#!/bin/sh\nset -e\necho "Running database migrations..."\nnpx prisma migrate deploy\necho "Starting application..."\nexec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh RUN printf '#!/bin/sh\nset -e\necho "Running database migrations..."\nnpx prisma migrate deploy\necho "Running database seed..."\nnpx prisma db seed || echo "Seed skipped or already applied"\necho "Starting application..."\nexec node dist/main.js\n' > /app/start.sh && chmod +x /app/start.sh
ENV NODE_ENV=production ENV NODE_ENV=production
ENV TZ=Asia/Shanghai ENV TZ=Asia/Shanghai

View File

@ -39,6 +39,9 @@
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.0" "swagger-ui-express": "^5.0.0"
}, },
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.2.1", "@nestjs/cli": "^10.2.1",
"@nestjs/schematics": "^10.0.3", "@nestjs/schematics": "^10.0.3",

View File

@ -313,7 +313,7 @@ export class ContributionWalletService {
province = await tx.province.create({ province = await tx.province.create({
data: { data: {
code: provinceCode, code: provinceCode,
name: `${provinceCode}`, // 默认名称,后续可更新 name: provinceCode,
status: 'ACTIVE', status: 'ACTIVE',
}, },
}); });
@ -342,36 +342,32 @@ export class ContributionWalletService {
if (!city) { if (!city) {
// 城市不存在,需要先有省份 // 城市不存在,需要先有省份
// 尝试从 cityCode 推断省份(如 GD-SZ 中的 GD if (!provinceCode) {
const parts = cityCode.split('-'); this.logger.warn(`Cannot create city without provinceCode: ${cityCode}`);
const inferredProvinceCode = parts.length > 1 ? parts[0] : null;
if (!inferredProvinceCode) {
this.logger.warn(`Cannot infer province from city code: ${cityCode}`);
return null; return null;
} }
// 找或创建省份 // 找或创建省份
let province = await tx.province.findUnique({ let province = await tx.province.findUnique({
where: { code: inferredProvinceCode }, where: { code: provinceCode },
}); });
if (!province) { if (!province) {
province = await tx.province.create({ province = await tx.province.create({
data: { data: {
code: inferredProvinceCode, code: provinceCode,
name: `${inferredProvinceCode}`, name: provinceCode,
status: 'ACTIVE', status: 'ACTIVE',
}, },
}); });
this.logger.log(`Auto-created province: ${inferredProvinceCode}`); this.logger.log(`Auto-created province: ${provinceCode}`);
} }
// 创建城市 // 创建城市
city = await tx.city.create({ city = await tx.city.create({
data: { data: {
code: cityCode, code: cityCode,
name: `${cityCode}`, // 默认名称 name: cityCode,
provinceId: province.id, provinceId: province.id,
status: 'ACTIVE', status: 'ACTIVE',
}, },