Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions backend/src/repository/dalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions backend/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
21 changes: 12 additions & 9 deletions frontend/src/app/components/ck-workspace/ck-workspace.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading