Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -900,10 +900,22 @@ export class PostsService {
content: removeLinks ? stripLinks(updateContent[i]) : updateContent[i],
}));

const scheduledDate =

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: On non-UTC servers, creating a scheduledDate with dayjs().format() and parsing it with dayjs.utc() causes an incorrect time comparison for posts with type: 'now'.
Severity: MEDIUM

Suggested Fix

Ensure timezone consistency. When generating the scheduledDate for type === 'now', use dayjs.utc() to create a UTC-based time string. Replace dayjs().format('YYYY-MM-DDTHH:mm:00') with dayjs.utc().format('YYYY-MM-DDTHH:mm:00') to align the generated time with the UTC-based comparison logic used later.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts#L903

Potential issue: A timezone mismatch occurs when editing a post with `body.type ===
'now'` on a server not running in the UTC timezone. The code generates a `scheduledDate`
using `dayjs().format()`, which creates a string representing the server's local time
but lacks a timezone offset. This string is later parsed by `dayjs.utc()`, which
incorrectly assumes the time is in UTC. This discrepancy causes the time comparison
against the existing `publishDate` to fail, leading to an unnecessary workflow restart
even when the scheduled time has not been modified.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with the new commit

body.type === 'now'
? dayjs().format('YYYY-MM-DDTHH:mm:00')
: body.date;

// Grab the current publish time BEFORE the upsert overwrites it, so we
// can tell (below) whether an edit actually moved the post's time.
const existingRootId = post.value?.[0]?.id;
const existingRoot = existingRootId
? await this._postRepository.getPostById(existingRootId, orgId)
: null;

Comment on lines +910 to +914

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially right — normal group edits update rows in place via upsert (ids preserved), so no new records are created and the existing workflow stays keyed to the same root; and when records genuinely are recreated, existingRoot is null so the skip can't fire. The real gap was an edit that promotes a child row to root (thread head removed/reordered) — fixed in 043e2da by requiring existingRoot to have no parentPostId.

const { posts } = await this._postRepository.createOrUpdatePost(
body.type,
orgId,
body.type === 'now' ? dayjs().format('YYYY-MM-DDTHH:mm:00') : body.date,
scheduledDate,
post,
body.tags,
creationMethod,
Expand All @@ -914,7 +926,24 @@ export class PostsService {
return [] as any[];
}

if (body.type !== 'update') {
// Only (re)start the Temporal workflow when it's actually needed: a new
// post, or a scheduled post whose publish time changed. Editing a queued
// post without moving its time keeps the existing workflow - content and
// settings are re-read from the DB at publish time, so a restart (which
// would terminate + recreate the workflow) is unnecessary churn.
// Only a 'schedule' save may skip: 'draft'/'now' saves must still go
// through startWorkflow so the old run is terminated. The existing row
// must also BE the root: if the edit promoted a child row (thread head
// removed/reordered), the workflow is keyed to the old root, which gets
// soft-deleted - the new root needs its own workflow.
const publishTimeUnchanged =
body.type === 'schedule' &&
!!existingRoot &&
!existingRoot.parentPostId &&
existingRoot.state === 'QUEUE' &&
dayjs(existingRoot.publishDate).isSame(dayjs(scheduledDate), 'minute');

if (body.type !== 'update' && !publishTimeUnchanged) {
Comment on lines +939 to +946

This comment was marked as outdated.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a followup commit

this.startWorkflow(
post.settings.__type.split('-')[0].toLowerCase(),
posts[0].id,
Expand Down
Loading