Skip to content

Commit

Permalink
Adding validation to update user
Browse files Browse the repository at this point in the history
  • Loading branch information
maayarosama committed Apr 23, 2024
1 parent d7715c5 commit 2933e2d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
16 changes: 11 additions & 5 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 @@ -250,6 +251,10 @@ export default {
const { execute, isLoading } = useAsyncState(
async () => {
console.log("image.value[0].name",image.value[0].name)
console.log("selectedUser.value.image",selectedUser.value.image)
selectedUser.value.user_type = selectedUser.value.user_type === "Team Lead" ? "Supervisor" : selectedUser.value.user_type
await $api.myprofile.update(selectedUser.value.id, {
...selectedUser.value,
Expand Down Expand Up @@ -288,6 +293,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 >= 3 && v.length <= 150) || 'Role must be between 3 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 2933e2d

Please sign in to comment.