107 lines
3.9 KiB
TypeScript
107 lines
3.9 KiB
TypeScript
import { db } from '../db/db_index'
|
|
import { eq, and } from 'drizzle-orm'
|
|
import type { UnitSeedData, StaffRoleName, StaffPermissionName, RolePermissionAssignment, KeyValSeedData } from '@packages/shared'
|
|
|
|
// ============================================================================
|
|
// Unit Seed Helper
|
|
// ============================================================================
|
|
|
|
export async function seedUnits(unitsToSeed: UnitSeedData[]): Promise<void> {
|
|
for (const unit of unitsToSeed) {
|
|
const { units: unitsTable } = await import('../db/schema')
|
|
const existingUnit = await db.query.units.findFirst({
|
|
where: eq(unitsTable.shortNotation, unit.shortNotation),
|
|
})
|
|
if (!existingUnit) {
|
|
await db.insert(unitsTable).values(unit)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Staff Role Seed Helper
|
|
// ============================================================================
|
|
|
|
export async function seedStaffRoles(rolesToSeed: StaffRoleName[]): Promise<void> {
|
|
for (const roleName of rolesToSeed) {
|
|
const { staffRoles } = await import('../db/schema')
|
|
const existingRole = await db.query.staffRoles.findFirst({
|
|
where: eq(staffRoles.roleName, roleName),
|
|
})
|
|
if (!existingRole) {
|
|
await db.insert(staffRoles).values({ roleName })
|
|
}
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Staff Permission Seed Helper
|
|
// ============================================================================
|
|
|
|
export async function seedStaffPermissions(permissionsToSeed: StaffPermissionName[]): Promise<void> {
|
|
for (const permissionName of permissionsToSeed) {
|
|
const { staffPermissions } = await import('../db/schema')
|
|
const existingPermission = await db.query.staffPermissions.findFirst({
|
|
where: eq(staffPermissions.permissionName, permissionName),
|
|
})
|
|
if (!existingPermission) {
|
|
await db.insert(staffPermissions).values({ permissionName })
|
|
}
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Role-Permission Assignment Helper
|
|
// ============================================================================
|
|
|
|
export async function seedRolePermissions(assignments: RolePermissionAssignment[]): Promise<void> {
|
|
await db.transaction(async (tx) => {
|
|
const { staffRoles, staffPermissions, staffRolePermissions } = await import('../db/schema')
|
|
|
|
for (const assignment of assignments) {
|
|
// Get role ID
|
|
const role = await tx.query.staffRoles.findFirst({
|
|
where: eq(staffRoles.roleName, assignment.roleName),
|
|
})
|
|
|
|
// Get permission ID
|
|
const permission = await tx.query.staffPermissions.findFirst({
|
|
where: eq(staffPermissions.permissionName, assignment.permissionName),
|
|
})
|
|
|
|
if (role && permission) {
|
|
const existing = await tx.query.staffRolePermissions.findFirst({
|
|
where: and(
|
|
eq(staffRolePermissions.staffRoleId, role.id),
|
|
eq(staffRolePermissions.staffPermissionId, permission.id)
|
|
),
|
|
})
|
|
if (!existing) {
|
|
await tx.insert(staffRolePermissions).values({
|
|
staffRoleId: role.id,
|
|
staffPermissionId: permission.id,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Key-Value Store Seed Helper
|
|
// ============================================================================
|
|
|
|
export async function seedKeyValStore(constantsToSeed: KeyValSeedData[]): Promise<void> {
|
|
for (const constant of constantsToSeed) {
|
|
const { keyValStore } = await import('../db/schema')
|
|
const existing = await db.query.keyValStore.findFirst({
|
|
where: eq(keyValStore.key, constant.key),
|
|
})
|
|
if (!existing) {
|
|
await db.insert(keyValStore).values({
|
|
key: constant.key,
|
|
value: constant.value,
|
|
})
|
|
}
|
|
}
|
|
}
|