Files
cfw-autumn/server/src/internal/entities/actions/updateEntityDbAndCache.ts
2026-03-30 15:25:40 +01:00

44 lines
1.1 KiB
TypeScript

import type { Entity } from "@autumn/shared";
import type { AutumnContext } from "@/honoUtils/HonoEnv.js";
import { EntityService } from "@/internal/api/entities/EntityService.js";
import { updateEntityInCache } from "@/internal/customers/cusUtils/fullCustomerCacheUtils/updateEntityInCache.js";
export const updateEntityDbAndCache = async ({
ctx,
customerId,
entity,
updates,
}: {
ctx: AutumnContext;
customerId: string;
entity: Entity;
updates: Partial<
Pick<Entity, "spend_limits" | "usage_alerts" | "overage_allowed">
>;
}) => {
const filteredUpdates = Object.fromEntries(
Object.entries(updates).filter(([, value]) => value !== undefined),
) as Partial<
Pick<Entity, "spend_limits" | "usage_alerts" | "overage_allowed">
>;
if (Object.keys(filteredUpdates).length === 0) {
return entity;
}
const updatedEntity = await EntityService.update({
db: ctx.db,
internalId: entity.internal_id,
update: filteredUpdates,
});
await updateEntityInCache({
ctx,
customerId,
idOrInternalId: entity.id ?? entity.internal_id,
updates: filteredUpdates,
});
return updatedEntity;
};