Skip to content

Commit

Permalink
Adding validation to update user (#400)
Browse files Browse the repository at this point in the history
* Adding validation to update user

* Removing logs

* Adjusting role validation rules
  • Loading branch information
maayarosama authored Apr 23, 2024
1 parent d7715c5 commit ba67808
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
16 changes: 9 additions & 7 deletions client/src/components/UpdateUser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
</v-col>
<v-col cols="6" v-if="selectedUser">
<v-text-field v-model="selectedUser.first_name" label="First Name" density="comfortable"
:rules="nameRules"></v-text-field>
:rules="[...requiredRules, ...lengthRules(45)]"></v-text-field>
<v-text-field v-model="selectedUser.last_name" label="Last Name" density="comfortable"
:rules="nameRules"></v-text-field>
:rules="[...requiredRules, ...lengthRules(45)]"></v-text-field>
<v-text-field v-model="selectedUser.email" label="Email" type="email" density="comfortable"
:rules="emailRules"></v-text-field>
:rules="[...emailRules, ...lengthRules(45)]"></v-text-field>
<v-text-field v-model="selectedUser.mobile_number" label="Mobile Number" type="number" density="comfortable"
:rules="mobileRules"></v-text-field>
<v-text-field v-model="selectedUser.job_title" label="Role" density="comfortable"
Expand All @@ -49,7 +49,7 @@
</v-col>
<v-col cols="6" v-if="selectedUser">
<v-text-field v-model="selectedUser.telegram_link" label="Telegram" density="comfortable"
:rules="requiredRules"></v-text-field>
:rules="[...requiredRules, ...lengthRules(100)]"></v-text-field>

<v-text-field v-model="selectedUser.birthday" label="Birthday" density="comfortable"
:rules="requiredStringRules" @click="toggleDatePicker('birthdayPicker')" readonly>
Expand All @@ -69,7 +69,7 @@
<v-date-picker v-model="joiningDate" v-if="joiningDatePicker"> </v-date-picker>

<v-select v-model="selectedUser.team" :items="teams" label="Team" density="comfortable"
:rules="requiredStringRules"></v-select>
:rules="[...requiredStringRules, ...lengthRules(30)]"></v-select>
<v-select v-model="selectedUser.gender" :items="genders" label="Gender" density="comfortable"
:rules="requiredStringRules"></v-select>
<v-select v-model="selectedUser.user_type" :items="types" label="User Type" density="comfortable"
Expand Down Expand Up @@ -114,6 +114,7 @@ import {
addressRules,
requiredStringRules,
requiredRules,
lengthRules,
formatDate,
listUsers
} from '@/utils'
Expand Down Expand Up @@ -249,11 +250,11 @@ export default {
)
const { execute, isLoading } = useAsyncState(
async () => {
async () => {
selectedUser.value.user_type = selectedUser.value.user_type === "Team Lead" ? "Supervisor" : selectedUser.value.user_type
await $api.myprofile.update(selectedUser.value.id, {
...selectedUser.value,
image: imageUrl.value ? imageUrl.value : selectedUser.value.image,
image: imageUrl.value ? imageUrl.value : null,
location: selectedUser.value.location.id,
filename: image.value ? image.value[0].name : null,
reporting_to: reporting_to.value ? [reporting_to.value.id] : []
Expand Down Expand Up @@ -288,6 +289,7 @@ export default {
supervisorPage,
supervisorCount,
requiredStringRules,
lengthRules,
requiredRules,
isLoading,
userIsActive,
Expand Down
9 changes: 7 additions & 2 deletions client/src/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ export const jobRules = [
(v: string) => typeof v === 'string' || 'Role must be a string.',
(v: string) => !!v || 'Role is required.',
(v: string) =>
(v && v.length >= 3 && v.length <= 50) || 'Role must be between 3 and 50 characters.'
(v && v.length >= 2 && v.length <= 150) || 'Role must be between 2 and 150 characters.'
]

export const addressRules = [
(v: string) => typeof v === 'string' || 'Address must be a string.',
(v: string) => !!v || 'Address is required.',
(v: string) =>
(v && v.length >= 3 && v.length <= 50) || 'Address must be between 3 and 50 characters.'
(v && v.length >= 3 && v.length <= 150) || 'Address must be between 3 and 150 characters.'
]
// export const socialInsuranceRules = [
// (v: string) => typeof v === 'string' || 'Social Number must be a string.',
Expand All @@ -56,3 +56,8 @@ export const vacationRules = [
(v: string) => +v <= 80 || 'The maximum allowed value is 80. ',
(v: string) => /^(?!00)\d{1,2}$/.test(v) || 'Invalid value.'
]

export const lengthRules = (maxValue : number) => [
(v: string) =>
(v && v.length >= 3 && v.length <= maxValue) || `The maximum allowed value is ${maxValue}.`
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.9 on 2024-04-23 09:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("cshr", "0008_alter_vacation_actual_days"),
]

operations = [
migrations.AlterField(
model_name="user",
name="gender",
field=models.CharField(
choices=[("Male", "male"), ("Female", "female")], max_length=30
),
),
migrations.AlterField(
model_name="user",
name="team",
field=models.CharField(
choices=[
("Business Development", "Businessdevelopment"),
("Development", "Development"),
("HR & Finance", "Hrandfinance"),
("QA", "Qa"),
("Marketing", "Marketing"),
("Operations", "Operations"),
("Support", "Support"),
],
max_length=30,
),
),
migrations.AlterField(
model_name="user",
name="user_type",
field=models.CharField(
choices=[
("Admin", "admin"),
("User", "user"),
("Supervisor", "supervisor"),
],
max_length=30,
),
),
]
6 changes: 3 additions & 3 deletions server/cshr/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ class User(AbstractBaseUser, TimeStamp):
telegram_link = models.CharField(max_length=100)
birthday = models.DateField()
joining_at = models.DateField()
team = models.CharField(max_length=20, choices=TEAM.choices)
team = models.CharField(max_length=30, choices=TEAM.choices)
salary = models.JSONField(default=dict, null=True, blank=True)
location = models.ForeignKey(Office, on_delete=models.CASCADE)
skills = models.ManyToManyField(UserSkills, related_name="skills", blank=True)
user_type = models.CharField(max_length=20, choices=USER_TYPE.choices)
gender = models.CharField(max_length=20, choices=GENDER_TYPE.choices)
user_type = models.CharField(max_length=30, choices=USER_TYPE.choices)
gender = models.CharField(max_length=30, choices=GENDER_TYPE.choices)
social_insurance_number = models.CharField(max_length=45, null=True, blank=True)
address = models.CharField(max_length=150)
job_title = models.CharField(max_length=150)
Expand Down

0 comments on commit ba67808

Please sign in to comment.