37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
import type { EntityManager } from '@mikro-orm/core';
|
|
import { User } from '../modules/users/entities/user.entity';
|
|
import { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
|
import { WalletTransaction } from '../modules/users/entities/wallet-transaction.entity';
|
|
import { WalletTransactionReason, WalletTransactionType } from 'src/modules/users/interface/wallet';
|
|
|
|
export class UserWalletsSeeder {
|
|
async run(em: EntityManager): Promise<void> {
|
|
const users = await em.find(User, {});
|
|
const restaurants = await em.find(Restaurant, {});
|
|
|
|
for (const user of users) {
|
|
for (const restaurant of restaurants) {
|
|
// Check if wallet already exists for this user-restaurant combination
|
|
const existingWallet = await em.findOne(WalletTransaction, {
|
|
user: { id: user.id },
|
|
restaurant: { id: restaurant.id },
|
|
});
|
|
|
|
if (!existingWallet) {
|
|
const wallet = em.create(WalletTransaction, {
|
|
user,
|
|
restaurant,
|
|
balance: 150000000,
|
|
amount: 150000000,
|
|
type: WalletTransactionType.CREDIT,
|
|
reason: WalletTransactionReason.DEPOSIT,
|
|
});
|
|
em.persist(wallet);
|
|
}
|
|
}
|
|
}
|
|
|
|
await em.flush();
|
|
}
|
|
}
|