diff --git a/libs/api/tracker/infrastructure/src/lib/mongo/automations.repository.ts b/libs/api/tracker/infrastructure/src/lib/mongo/automations.repository.ts index 56f1a9c..f97af80 100644 --- a/libs/api/tracker/infrastructure/src/lib/mongo/automations.repository.ts +++ b/libs/api/tracker/infrastructure/src/lib/mongo/automations.repository.ts @@ -30,21 +30,21 @@ export class MongoAutomationsRepository extends AutomationsRepository { .find({ projectId }) .sort({ createdAt: 1 }) .lean(); - return docs.map((doc) => fromDoc(doc) as Automation); + return docs.map(toAutomation); } async findActive(projectId: string): Promise { const docs = await this.automations .find({ projectId, active: true }) .lean(); - return docs.map((doc) => fromDoc(doc) as Automation); + return docs.map(toAutomation); } async findById(id: string): Promise { const doc = await this.automations .findOne({ id: id }) .lean(); - return doc ? (fromDoc(doc) as Automation) : null; + return doc ? toAutomation(doc) : null; } async insert({ id, ...rest }: Automation): Promise { @@ -83,3 +83,13 @@ export class MongoAutomationsRepository extends AutomationsRepository { ); } } + +/** + * Mongo leaves empty objects out (`minimize`): a rule saved with no + * conditions comes back without them, and both GraphQL and the rules + * engine need the object. + */ +function toAutomation(doc: AutomationDoc): Automation { + const automation = fromDoc(doc) as Automation; + return { ...automation, conditions: automation.conditions ?? {} }; +}