diff --git a/backend/src/repository/dalPost.ts b/backend/src/repository/dalPost.ts index ca73b02e..55b6587f 100644 --- a/backend/src/repository/dalPost.ts +++ b/backend/src/repository/dalPost.ts @@ -7,6 +7,8 @@ import { Options } from '../utils/api.helpers'; import dalBucket from './dalBucket'; import dalComment from './dalComment'; import dalVote from './dalVote'; +import User, { Role } from '../models/User'; +import { STUDENT_POST_COLOR, TEACHER_POST_COLOR } from '../utils/Utils'; export const getById = async (id: string) => { try { @@ -63,6 +65,34 @@ export const getByBucket = async (bucketID: string, opts?: Options) => { }; export const create = async (post: PostModel) => { + // 1. Determine the correct default color by looking up the user's role. + let defaultFillColor = STUDENT_POST_COLOR; // Default to the most common color. + try { + const user = await User.findOne({ userID: post.userID }).select('role').lean(); + if (user?.role === Role.TEACHER) { + defaultFillColor = TEACHER_POST_COLOR; + } + } catch (e) { + console.error("Could not fetch user role for default color. Falling back to default.", e); + } + + // 2. Define the complete default structure using our dynamic color. + const defaultDisplayAttributes = { + position: { left: 150, top: 150 }, + fillColor: defaultFillColor, + lock: false, + }; + + // 3. Safely merge the incoming post's displayAttributes with the defaults. + // The incoming 'post' values will overwrite our defaults if they exist. + post.displayAttributes = { + ...defaultDisplayAttributes, // Establishes our smart defaults (including the correct color) + ...post.displayAttributes, // Client-provided attributes (like a specific color) overwrite the defaults + position: { + ...defaultDisplayAttributes.position, // Ensures position object has defaults + ...(post.displayAttributes?.position), // Client-provided position values overwrite default position + }, + }; try { const savedPost = await Post.create(post); return savedPost; diff --git a/backend/src/utils/Utils.ts b/backend/src/utils/Utils.ts index 39029c3e..f2863b76 100644 --- a/backend/src/utils/Utils.ts +++ b/backend/src/utils/Utils.ts @@ -3,3 +3,6 @@ import { v4 as uuidv4 } from 'uuid'; export const generateUniqueID = () => { return uuidv4(); }; + +export const STUDENT_POST_COLOR = '#FFF7C0'; +export const TEACHER_POST_COLOR = '#BBC4F7'; \ No newline at end of file diff --git a/frontend/src/app/components/ck-workspace/ck-workspace.component.ts b/frontend/src/app/components/ck-workspace/ck-workspace.component.ts index c55d8138..b0a128ad 100644 --- a/frontend/src/app/components/ck-workspace/ck-workspace.component.ts +++ b/frontend/src/app/components/ck-workspace/ck-workspace.component.ts @@ -944,19 +944,22 @@ export class CkWorkspaceComponent implements OnInit, OnDestroy { const destinationType = PostType[this.runningGroupTask?.workflow.destinations[0].type]; post.type = destinationType; + // 1. Create and assign displayAttributes for ALL posts unconditionally. + const displayAttributes: DisplayAttributes = { + position: { + left: 150, + top: 150, + }, + lock: !this.board.permissions.allowStudentMoveAny, + fillColor: this.defaultPostFill(), + }; + post.displayAttributes = displayAttributes; + + // 2. The if/else block now only needs to handle properties that are different, like the boardID. if (destinationType === PostType.BUCKET) { post.boardID = this.board.boardID; } else { - const displayAttributes: DisplayAttributes = { - position: { - left: 150, - top: 150, - }, - lock: !this.board.permissions.allowStudentMoveAny, - fillColor: this.defaultPostFill(), - }; post.boardID = this.runningGroupTask?.workflow.destinations[0].id; - post.displayAttributes = displayAttributes; } const htmlPost = await this.converters.toHTMLPost(post); this.submittedPosts.push(htmlPost);