来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
191 lines
6.0 KiB
TypeScript
191 lines
6.0 KiB
TypeScript
import type { Pool } from 'pg';
|
|
import { hashPassword } from './utils/password';
|
|
|
|
/** 生产/测试环境统一 E2E 账号密码,每次 migrate 同步 */
|
|
export const E2E_TEST_PASSWORD = process.env.E2E_TEST_PASSWORD || 'E2eTest@123';
|
|
|
|
const DEFAULT_BRANCH = process.env.E2E_TEST_BRANCH || 'BR002';
|
|
|
|
type StaffAccount = {
|
|
id: string;
|
|
email: string;
|
|
username: string;
|
|
fullName: string;
|
|
role: string;
|
|
};
|
|
|
|
const STAFF_ACCOUNTS: StaffAccount[] = [
|
|
{
|
|
id: 'a0000000-0000-0000-0000-000000000002',
|
|
email: 'admin@meoo.local',
|
|
username: 'admin',
|
|
fullName: '系统管理员',
|
|
role: 'admin',
|
|
},
|
|
{
|
|
id: 'a0000000-0000-0000-0000-000000000003',
|
|
email: 'operator@meoo.local',
|
|
username: 'operator',
|
|
fullName: '运营专员',
|
|
role: 'operator',
|
|
},
|
|
{
|
|
id: 'a0000000-0000-0000-0000-000000000004',
|
|
email: 'finance@meoo.local',
|
|
username: 'finance',
|
|
fullName: '财务专员',
|
|
role: 'finance',
|
|
},
|
|
{
|
|
id: 'a0000000-0000-0000-0000-000000000005',
|
|
email: 'warehouse@meoo.local',
|
|
username: 'warehouse',
|
|
fullName: '仓库专员',
|
|
role: 'warehouse',
|
|
},
|
|
{
|
|
id: 'a0000000-0000-0000-0000-000000000006',
|
|
email: 'delivery@meoo.local',
|
|
username: 'delivery',
|
|
fullName: '配送专员',
|
|
role: 'delivery',
|
|
},
|
|
];
|
|
|
|
async function ensureAuthUser(
|
|
pool: Pool,
|
|
params: { id: string; email: string; username: string; fullName: string; passwordHash: string }
|
|
) {
|
|
await pool.query(
|
|
`INSERT INTO auth.users (
|
|
instance_id, id, aud, role, email, encrypted_password, email_confirmed_at,
|
|
raw_app_meta_data, raw_user_meta_data
|
|
) VALUES (
|
|
'00000000-0000-0000-0000-000000000000', $1, 'authenticated', 'authenticated', $2,
|
|
$3, NOW(),
|
|
'{"provider":"email","providers":["email"]}'::jsonb, $4::jsonb
|
|
)
|
|
ON CONFLICT (email) DO UPDATE SET
|
|
encrypted_password = EXCLUDED.encrypted_password,
|
|
raw_user_meta_data = EXCLUDED.raw_user_meta_data`,
|
|
[
|
|
params.id,
|
|
params.email,
|
|
params.passwordHash,
|
|
JSON.stringify({ username: params.username, full_name: params.fullName }),
|
|
]
|
|
);
|
|
await pool.query(
|
|
`INSERT INTO profiles (id, username, full_name, branch_code, status, must_change_password)
|
|
VALUES ($1, $2, $3, $4, 1, false)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
username = EXCLUDED.username,
|
|
full_name = EXCLUDED.full_name,
|
|
branch_code = COALESCE(profiles.branch_code, EXCLUDED.branch_code),
|
|
status = 1,
|
|
must_change_password = false`,
|
|
[params.id, params.username, params.fullName, DEFAULT_BRANCH]
|
|
);
|
|
}
|
|
|
|
async function ensureRole(pool: Pool, userId: string, role: string) {
|
|
await pool.query(
|
|
`INSERT INTO user_roles (user_id, role)
|
|
VALUES ($1, $2::public.app_role)
|
|
ON CONFLICT (user_id, role) DO NOTHING`,
|
|
[userId, role]
|
|
);
|
|
}
|
|
|
|
export async function seedE2eRolePasswords(pool: Pool) {
|
|
const passwordHash = await hashPassword(E2E_TEST_PASSWORD);
|
|
|
|
await pool.query(
|
|
`INSERT INTO branches (branch_code, branch_name, status)
|
|
VALUES ($1, 'E2E测试分公司', 1)
|
|
ON CONFLICT (branch_code) DO NOTHING`,
|
|
[DEFAULT_BRANCH]
|
|
);
|
|
|
|
for (const account of STAFF_ACCOUNTS) {
|
|
await ensureAuthUser(pool, {
|
|
id: account.id,
|
|
email: account.email,
|
|
username: account.username,
|
|
fullName: account.fullName,
|
|
passwordHash,
|
|
});
|
|
await ensureRole(pool, account.id, account.role);
|
|
console.log(`E2E staff ready: ${account.username} / ${E2E_TEST_PASSWORD}`);
|
|
}
|
|
|
|
const purchaserId = 'b0000000-0000-0000-0000-000000000001';
|
|
await ensureAuthUser(pool, {
|
|
id: purchaserId,
|
|
email: 'purchaser1@purchaser.meoo.local',
|
|
username: 'purchaser1',
|
|
fullName: 'E2E采购员',
|
|
passwordHash,
|
|
});
|
|
await pool.query(
|
|
`INSERT INTO purchaser_users (user_id, purchaser_code, purchaser_name, username, is_primary, status)
|
|
VALUES ($1, 'PUR001', 'E2E采购员', 'purchaser1', true, 1)
|
|
ON CONFLICT (purchaser_code) DO UPDATE SET
|
|
user_id = EXCLUDED.user_id,
|
|
purchaser_name = EXCLUDED.purchaser_name,
|
|
username = EXCLUDED.username,
|
|
status = 1`,
|
|
[purchaserId]
|
|
);
|
|
console.log(`E2E mobile ready: purchaser1 / ${E2E_TEST_PASSWORD}`);
|
|
|
|
const supplierId = 'b0000000-0000-0000-0000-000000000002';
|
|
await ensureAuthUser(pool, {
|
|
id: supplierId,
|
|
email: 'supplier1@supplier.meoo.local',
|
|
username: 'supplier1',
|
|
fullName: 'E2E供应商',
|
|
passwordHash,
|
|
});
|
|
await pool.query(
|
|
`INSERT INTO suppliers (supplier_code, company_name, status)
|
|
VALUES ('SUP001', 'E2E演示供应商', 10)
|
|
ON CONFLICT (supplier_code) DO NOTHING`
|
|
);
|
|
await pool.query(`DELETE FROM supplier_users WHERE supplier_code = 'SUP001' OR user_id = $1`, [supplierId]);
|
|
await pool.query(
|
|
`INSERT INTO supplier_users (user_id, supplier_code, username, status)
|
|
VALUES ($1, 'SUP001', 'supplier1', 1)`,
|
|
[supplierId]
|
|
);
|
|
console.log(`E2E mobile ready: supplier1 / ${E2E_TEST_PASSWORD}`);
|
|
|
|
await pool.query(
|
|
`INSERT INTO delivery_persons (person_code, person_name, phone, status)
|
|
VALUES ('DP001', 'E2E配送员', '13800000099', 10)
|
|
ON CONFLICT (person_code) DO NOTHING`
|
|
);
|
|
const deliveryId = 'b0000000-0000-0000-0000-000000000003';
|
|
await ensureAuthUser(pool, {
|
|
id: deliveryId,
|
|
email: 'delivery1@delivery.meoo.local',
|
|
username: 'delivery1',
|
|
fullName: 'E2E配送员',
|
|
passwordHash,
|
|
});
|
|
await pool.query(`DELETE FROM delivery_person_users WHERE person_code = 'DP001' OR user_id = $1`, [deliveryId]);
|
|
await pool.query(
|
|
`INSERT INTO delivery_person_users (user_id, person_code, username, status)
|
|
VALUES ($1, 'DP001', 'delivery1', 1)`,
|
|
[deliveryId]
|
|
);
|
|
console.log(`E2E mobile ready: delivery1 / ${E2E_TEST_PASSWORD}`);
|
|
|
|
const synced = await pool.query(
|
|
`UPDATE auth.users u SET encrypted_password = $1
|
|
FROM profiles p WHERE p.id = u.id AND p.username = ANY($2::text[])`,
|
|
[passwordHash, STAFF_ACCOUNTS.map((a) => a.username).concat(['purchaser1', 'supplier1', 'delivery1'])]
|
|
);
|
|
console.log(`E2E password batch sync: ${synced.rowCount || 0} users`);
|
|
}
|