games_app/backend/dist-test-faq/repositories/mobileAppReleaseRepository.js

255 lines
11 KiB
JavaScript

"use strict";
/**
* Persistence for mobile app releases, settings, and telemetry events.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSettings = getSettings;
exports.updateSettings = updateSettings;
exports.createRelease = createRelease;
exports.findById = findById;
exports.listReleases = listReleases;
exports.findByPlatformChannelVersionCode = findByPlatformChannelVersionCode;
exports.updateRelease = updateRelease;
exports.findLatestPublished = findLatestPublished;
exports.deleteEventsForRelease = deleteEventsForRelease;
exports.deleteRelease = deleteRelease;
exports.findHighestMandatoryPublished = findHighestMandatoryPublished;
exports.incrementCounter = incrementCounter;
exports.insertEvent = insertEvent;
exports.getStatsSummary = getStatsSummary;
const connection_1 = require("../db/connection");
function mapRelease(row) {
return {
id: row.id,
platform: row.platform,
channel: row.channel,
versionName: row.version_name,
versionCode: Number(row.version_code),
packageName: row.package_name,
previousVersionName: row.previous_version_name,
whatsNew: row.whats_new,
bugFixes: row.bug_fixes,
status: row.status,
updatePolicy: row.update_policy,
rolloutPercent: Number(row.rollout_percent),
storageKey: row.storage_key,
fileSizeBytes: row.file_size_bytes == null ? null : Number(row.file_size_bytes),
sha256: row.sha256,
signingCertSha256: row.signing_cert_sha256,
publishedAt: row.published_at,
createdBy: row.created_by,
downloadCount: Number(row.download_count),
downloadFailCount: Number(row.download_fail_count),
installConfirmCount: Number(row.install_confirm_count),
createdAt: row.created_at,
updatedAt: row.updated_at,
};
}
const RELEASE_COLS = `
id, platform, channel, version_name, version_code, package_name, previous_version_name,
whats_new, bug_fixes, status, update_policy, rollout_percent, storage_key, file_size_bytes,
sha256, signing_cert_sha256, published_at, created_by, download_count, download_fail_count,
install_confirm_count, created_at, updated_at
`;
async function getSettings() {
const rows = await (0, connection_1.query)(`SELECT id, min_supported_version_code, check_interval_seconds, expected_package_name, updated_at
FROM mobile_app_update_settings WHERE id = 1`);
const row = rows[0];
if (!row) {
return {
id: 1,
minSupportedVersionCode: 1,
checkIntervalSeconds: 21600,
expectedPackageName: 'com.prediqo.mobile',
updatedAt: new Date(),
};
}
return {
id: row.id,
minSupportedVersionCode: Number(row.min_supported_version_code),
checkIntervalSeconds: Number(row.check_interval_seconds),
expectedPackageName: row.expected_package_name,
updatedAt: row.updated_at,
};
}
async function updateSettings(input) {
await (0, connection_1.query)(`UPDATE mobile_app_update_settings SET
min_supported_version_code = COALESCE($1, min_supported_version_code),
check_interval_seconds = COALESCE($2, check_interval_seconds),
expected_package_name = COALESCE($3, expected_package_name),
updated_at = NOW()
WHERE id = 1`, [
input.minSupportedVersionCode ?? null,
input.checkIntervalSeconds ?? null,
input.expectedPackageName ?? null,
]);
return getSettings();
}
async function createRelease(input) {
const rows = await (0, connection_1.query)(`INSERT INTO mobile_app_releases (
platform, channel, version_name, version_code, previous_version_name,
whats_new, bug_fixes, update_policy, rollout_percent, created_by
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
RETURNING ${RELEASE_COLS}`, [
input.platform,
input.channel,
input.versionName,
input.versionCode,
input.previousVersionName ?? null,
input.whatsNew ?? '',
input.bugFixes ?? '',
input.updatePolicy ?? 'optional',
input.rolloutPercent ?? 100,
input.createdBy ?? null,
]);
return mapRelease(rows[0]);
}
async function findById(id) {
const rows = await (0, connection_1.query)(`SELECT ${RELEASE_COLS} FROM mobile_app_releases WHERE id = $1`, [id]);
return rows[0] ? mapRelease(rows[0]) : null;
}
async function listReleases(filters) {
const where = [];
const params = [];
let i = 1;
if (filters.platform) {
where.push(`platform = $${i++}`);
params.push(filters.platform);
}
if (filters.channel) {
where.push(`channel = $${i++}`);
params.push(filters.channel);
}
if (filters.status) {
where.push(`status = $${i++}`);
params.push(filters.status);
}
if (filters.search?.trim()) {
where.push(`(version_name ILIKE $${i} OR CAST(version_code AS TEXT) ILIKE $${i})`);
params.push(`%${filters.search.trim()}%`);
i++;
}
const whereSql = where.length ? `WHERE ${where.join(' AND ')}` : '';
const limit = Math.min(Math.max(filters.limit ?? 50, 1), 200);
const offset = Math.max(filters.offset ?? 0, 0);
const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::int AS c FROM mobile_app_releases ${whereSql}`, params);
const total = Number(countRows[0]?.c ?? 0);
const rows = await (0, connection_1.query)(`SELECT ${RELEASE_COLS} FROM mobile_app_releases ${whereSql}
ORDER BY version_code DESC, created_at DESC
LIMIT $${i++} OFFSET $${i++}`, [...params, limit, offset]);
return { items: rows.map(mapRelease), total };
}
async function findByPlatformChannelVersionCode(platform, channel, versionCode) {
const rows = await (0, connection_1.query)(`SELECT ${RELEASE_COLS} FROM mobile_app_releases
WHERE platform = $1 AND channel = $2 AND version_code = $3
LIMIT 1`, [platform, channel, versionCode]);
return rows[0] ? mapRelease(rows[0]) : null;
}
async function updateRelease(id, patch) {
const rows = await (0, connection_1.query)(`UPDATE mobile_app_releases SET
version_name = COALESCE($2, version_name),
version_code = COALESCE($3, version_code),
previous_version_name = CASE WHEN $4::boolean THEN $5 ELSE previous_version_name END,
whats_new = COALESCE($6, whats_new),
bug_fixes = COALESCE($7, bug_fixes),
update_policy = COALESCE($8, update_policy),
rollout_percent = COALESCE($9, rollout_percent),
status = COALESCE($10, status),
package_name = CASE WHEN $11::boolean THEN $12 ELSE package_name END,
storage_key = CASE WHEN $13::boolean THEN $14 ELSE storage_key END,
file_size_bytes = CASE WHEN $15::boolean THEN $16 ELSE file_size_bytes END,
sha256 = CASE WHEN $17::boolean THEN $18 ELSE sha256 END,
signing_cert_sha256 = CASE WHEN $19::boolean THEN $20 ELSE signing_cert_sha256 END,
published_at = CASE WHEN $21::boolean THEN $22 ELSE published_at END,
updated_at = NOW()
WHERE id = $1
RETURNING ${RELEASE_COLS}`, [
id,
patch.versionName ?? null,
patch.versionCode ?? null,
patch.previousVersionName !== undefined,
patch.previousVersionName ?? null,
patch.whatsNew ?? null,
patch.bugFixes ?? null,
patch.updatePolicy ?? null,
patch.rolloutPercent ?? null,
patch.status ?? null,
patch.packageName !== undefined,
patch.packageName ?? null,
patch.storageKey !== undefined,
patch.storageKey ?? null,
patch.fileSizeBytes !== undefined,
patch.fileSizeBytes ?? null,
patch.sha256 !== undefined,
patch.sha256 ?? null,
patch.signingCertSha256 !== undefined,
patch.signingCertSha256 ?? null,
patch.publishedAt !== undefined,
patch.publishedAt ?? null,
]);
return rows[0] ? mapRelease(rows[0]) : null;
}
async function findLatestPublished(platform, channel) {
const rows = await (0, connection_1.query)(`SELECT ${RELEASE_COLS} FROM mobile_app_releases
WHERE platform = $1 AND channel = $2 AND status = 'published'
ORDER BY version_code DESC
LIMIT 1`, [platform, channel]);
return rows[0] ? mapRelease(rows[0]) : null;
}
/** Deletes telemetry events for a release (hard delete cleanup). */
async function deleteEventsForRelease(releaseId) {
const rows = await (0, connection_1.query)(`DELETE FROM mobile_app_release_events WHERE release_id = $1 RETURNING id`, [releaseId]);
return rows.length;
}
/** Permanently removes a release row. Returns false if not found. */
async function deleteRelease(id) {
const rows = await (0, connection_1.query)(`DELETE FROM mobile_app_releases WHERE id = $1 RETURNING id`, [id]);
return Boolean(rows[0]);
}
async function findHighestMandatoryPublished(platform, channel) {
const rows = await (0, connection_1.query)(`SELECT ${RELEASE_COLS} FROM mobile_app_releases
WHERE platform = $1 AND channel = $2 AND status = 'published' AND update_policy = 'mandatory'
ORDER BY version_code DESC
LIMIT 1`, [platform, channel]);
return rows[0] ? mapRelease(rows[0]) : null;
}
async function incrementCounter(id, field) {
await (0, connection_1.query)(`UPDATE mobile_app_releases SET ${field} = ${field} + 1, updated_at = NOW() WHERE id = $1`, [id]);
}
async function insertEvent(input) {
await (0, connection_1.query)(`INSERT INTO mobile_app_release_events (
release_id, event_type, platform, channel, client_version_code, device_id_hash, user_id, meta
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8::jsonb)`, [
input.releaseId ?? null,
input.eventType,
input.platform ?? 'android',
input.channel ?? 'production',
input.clientVersionCode ?? null,
input.deviceIdHash ?? null,
input.userId ?? null,
JSON.stringify(input.meta ?? {}),
]);
}
async function getStatsSummary(platform = 'android') {
const statusRows = await (0, connection_1.query)(`SELECT status, COUNT(*)::int AS c FROM mobile_app_releases WHERE platform = $1 GROUP BY status`, [platform]);
const versionRows = await (0, connection_1.query)(`SELECT id, version_name, version_code, status, download_count, download_fail_count, install_confirm_count
FROM mobile_app_releases WHERE platform = $1
ORDER BY version_code DESC LIMIT 50`, [platform]);
const eventRows = await (0, connection_1.query)(`SELECT event_type, COUNT(*)::int AS c FROM mobile_app_release_events
WHERE platform = $1 AND created_at > NOW() - INTERVAL '30 days'
GROUP BY event_type`, [platform]);
return {
byStatus: statusRows.map((r) => ({ status: r.status, count: Number(r.c) })),
byVersion: versionRows.map((r) => ({
id: r.id,
versionName: r.version_name,
versionCode: Number(r.version_code),
status: r.status,
downloadCount: Number(r.download_count),
downloadFailCount: Number(r.download_fail_count),
installConfirmCount: Number(r.install_confirm_count),
})),
eventCounts: eventRows.map((r) => ({ eventType: r.event_type, count: Number(r.c) })),
};
}
//# sourceMappingURL=mobileAppReleaseRepository.js.map