Notification send based on certain Person

Dear Community,

I had two doctypes
a.snag
b.department

a.Snag Doctype
Fields as
1.Department (link) as department,option as Department
2.Assigned To (link) as assigned_to , option as User
3.Status(select) as status

b.Department Doctype
Fields
1.Responsible Person (Link) as custom_responsible_person,option as User

In Department i assigned Responsible person for particular department

based on that Department particular person assigned for particular department

Here once we enter department notification send via email and system notification for certain assigned person also once he assigned to user same method need for user then finally notification of status need to received,

I tried custom script but it only show the message sent but no notification received by responsible person.

Code:
Notify the Responsible Person when a Snag is Created

frappe.ui.form.on('Snag', {
    after_save: function(frm) {
        if (frm.doc.department) {
            // Fetch Responsible Person from Department
            frappe.call({
                method: "frappe.client.get_value",
                args: {
                    doctype: "Department",
                    fieldname: "custom_responsible_person",
                    filters: { name: frm.doc.department }
                },
                callback: function(r) {
                    if (r.message && r.message.custom_responsible_person) {
                        let responsible_person = r.message.custom_responsible_person;

                        // Send email to Responsible Person
                        frappe.call({
                            method: "frappe.core.doctype.communication.email.make",
                            args: {
                                recipients: responsible_person,
                                subject: `New Snag Created in Department ${frm.doc.department}`,
                                content: `A new snag has been reported in the department ${frm.doc.department}. Please review and assign the task.`,
                                doctype: frm.doc.doctype,
                                name: frm.doc.name,
                                send_email: true
                            }
                        });

                        // Show in-system notification
                        frappe.show_alert({
                            message: `A new snag has been created for the department ${frm.doc.department}. Please review it.`,
                            indicator: 'blue'
                        });

                        frappe.msgprint(`Notification sent to the Responsible Person for department ${frm.doc.department}.`);
                    }
                }
            });
        }
    }
});

Kindly help to solve this issue

But you can easily set from the server side.
Reference: Utility Functions

thanks for the response i tried in server script it mostly works except one condition

notification is received correctly

one the responsible person assign to assigned user notification sent correctly but when assigned user update the status notiifcation send himself instead of Responsible person who assigned to him.

if doc.department:
    # Get the assigned person first
    assigned_person = doc.assigned_to  # Assuming `assigned_to` is the field name

    # Retrieve responsible person for the department
    responsible_person = frappe.db.get_value('Department', doc.department, 'custom_responsible_person')

    if assigned_person:
        try:
            # Create system notification for assigned user
            notification = frappe.new_doc('Notification Log')
            notification.update({
                'subject': f"New Snag Assignment - {doc.department}",
                'for_user': assigned_person,  # Send notification to assigned person
                'type': "Alert",
                'document_type': doc.doctype,
                'document_name': doc.name,
                'email_content': f"New snag assigned to you from department: {doc.department}"
            })
            notification.insert(ignore_permissions=True)

            # Show confirmation message
            frappe.msgprint(f"System notification sent to {assigned_person}")

            # Send email notification to assigned person
            frappe.sendmail(
                recipients=[assigned_person],
                subject=f"New Snag Assignment - {doc.department}",
                message=f"""
                Dear {frappe.db.get_value('User', assigned_person, 'full_name')},

                A new snag has been assigned to you.

                Department: {doc.department}
                Status: {doc.status}

                Please review and take necessary action.

                You can view the snag here: {frappe.utils.get_url_to_form(doc.doctype, doc.name)}
                """
            )
            frappe.msgprint("Email notification sent to assigned person")

        except Exception as e:
            frappe.msgprint(f"Error in notification: {str(e)}")
            frappe.log_error(str(e), "Snag Notification Error")

    else:
        # If there is no assigned person, fall back to responsible person
        if responsible_person:
            try:
                # Create system notification for responsible person
                notification = frappe.new_doc('Notification Log')
                notification.update({
                    'subject': f"New Snag Assignment - {doc.department}",
                    'for_user': responsible_person,
                    'type': "Alert",
                    'document_type': doc.doctype,
                    'document_name': doc.name,
                    'email_content': f"New snag assigned to your department: {doc.department}"
                })
                notification.insert(ignore_permissions=True)

                # Show confirmation message
                frappe.msgprint(f"System notification sent to {responsible_person}")

                # Check if the responsible person is updating the snag
                if doc.updated_by == responsible_person:
                    # Do not send an email if the responsible person updates without assigning to someone
                    if not doc.assigned_to:  # If there's no assigned user
                        frappe.msgprint("No email sent as you are the one updating the snag.")
                    else:
                        # Send email notification to responsible person if they assign it
                        frappe.sendmail(
                            recipients=[responsible_person],
                            subject=f"New Snag Assignment - {doc.department}",
                            message=f"""
                            Dear {frappe.db.get_value('User', responsible_person, 'full_name')},

                            A new snag has been assigned to your department.

                            Department: {doc.department}
                            Status: {doc.status}

                            Please review and assign to appropriate team member.

                            You can view the snag here: {frappe.utils.get_url_to_form(doc.doctype, doc.name)}
                            """
                        )
                        frappe.msgprint("Email notification sent to responsible person")
                else:
                    # If it's not the responsible person, send the notification as usual
                    frappe.sendmail(
                        recipients=[responsible_person],
                        subject=f"New Snag Assignment - {doc.department}",
                        message=f"""
                        Dear {frappe.db.get_value('User', responsible_person, 'full_name')},

                        A new snag has been assigned to your department.

                        Department: {doc.department}
                        Status: {doc.status}

                        Please review and assign to appropriate team member.

                        You can view the snag here: {frappe.utils.get_url_to_form(doc.doctype, doc.name)}
                        """
                    )
                    frappe.msgprint("Email notification sent to responsible person")

            except Exception as e:
                frappe.msgprint(f"Error in notification: {str(e)}")
                frappe.log_error(str(e), "Snag Notification Error")
        else:
            frappe.msgprint("No responsible person found for this department!")

# Logic to notify the responsible person when the assigned user updates the snag
if doc.status_changed:  # Assuming `status_changed` is a flag indicating the status was updated
    if assigned_person and doc.updated_by == assigned_person:  # Check if the updater is the assigned person
        if responsible_person:
            try:
                # Create notification for responsible person
                notification = frappe.new_doc('Notification Log')
                notification.update({
                    'subject': f"Snag Updated - {doc.department}",
                    'for_user': responsible_person,
                    'type': "Alert",
                    'document_type': doc.doctype,
                    'document_name': doc.name,
                    'email_content': f"The snag assigned to {assigned_person} has been updated."
                })
                notification.insert(ignore_permissions=True)

                # Send email notification to responsible person
                frappe.sendmail(
                    recipients=[responsible_person],
                    subject=f"Snag Updated - {doc.department}",
                    message=f"""
                    Dear {frappe.db.get_value('User', responsible_person, 'full_name')},

                    The snag assigned to {assigned_person} has been updated.

                    Department: {doc.department}
                    Status: {doc.status}

                    You can view the snag here: {frappe.utils.get_url_to_form(doc.doctype, doc.name)}
                    """
                )
                frappe.msgprint(f"Email notification sent to responsible person {responsible_person}")

            except Exception as e:
                frappe.msgprint(f"Error in notification: {str(e)}")
                frappe.log_error(str(e), "Snag Notification Error")