Search code examples
javascriptwakanda

Wakanda : Modify field on entry creation


I am currently making an app for punching in and out employees.

I have three tables, one for the Employes, one for the PunchIn and one for the PunchOut.

Diagram

What I want is the following : when I create a PunchIn, it change the is_in entry (of the table Employees) to true and when I create a PunchOut, the is_in is false.

How can I deal with relations to make this?

Thanks!


Solution

  • Assuming the employee entity already exists, it's just:

    punchInEntity.Employee.is_in = true;
    

    and

    punchOutEntity.Employee.is_in = false;
    

    Be sure to save both entities afterward:

    punchOutEntity.save();
    punchOutEntity.Employee.save();
    

    However, you might want to consider making is_in a computed attribute. In it's method, you could search for a punchOutCollection entity that is after the current date/time. Return false if one is found and true if not (as long as there is at least one punchInCollection entity). That way, you don't have to worry about updating is_in and it will always be correct.