Skip to content

Commit

Permalink
Require phone number for all accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Dec 22, 2024
1 parent 2651306 commit d189b8a
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 24 deletions.
10 changes: 5 additions & 5 deletions app/resident_manager/lib/src/models/info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PersonalInfo {
final Date? birthday;

/// The target's phone number.
final String? phone;
final String phone;

/// The target's email address.
final String? email;
Expand All @@ -24,7 +24,7 @@ class PersonalInfo {
required this.name,
required this.room,
this.birthday,
this.phone,
required this.phone,
this.email,
});

Expand Down Expand Up @@ -52,7 +52,7 @@ class PersonalInfo {
"name": name,
"room": room.toString(),
if (birthday != null) "birthday": birthday!.format("yyyy-mm-dd"),
if (phone != null) "phone": phone!,
"phone": phone,
if (email != null) "email": email!,
};
}
Expand All @@ -75,7 +75,7 @@ class PublicInfo extends PersonalInfo with Snowflake {
required super.name,
required super.room,
super.birthday,
super.phone,
required super.phone,
super.email,
this.username,
this.hashedPassword,
Expand All @@ -88,7 +88,7 @@ class PublicInfo extends PersonalInfo with Snowflake {
name: data["name"] as String,
room: data["room"] as int,
birthday: data["birthday"] == null ? null : Date.parse(data["birthday"] as String),
phone: data["phone"] as String?,
phone: data["phone"] as String,
email: data["email"] as String?,
username: data["username"] as String?,
hashedPassword: data["hashed_password"] as String?,
Expand Down
2 changes: 1 addition & 1 deletion app/resident_manager/lib/src/models/reg_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RegisterRequest extends PublicInfo {
required super.name,
required super.room,
super.birthday,
super.phone,
required super.phone,
super.email,
super.username,
super.hashedPassword,
Expand Down
2 changes: 1 addition & 1 deletion app/resident_manager/lib/src/models/residents.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Resident extends PublicInfo {
required super.name,
required super.room,
super.birthday,
super.phone,
required super.phone,
super.email,
super.username,
super.hashedPassword,
Expand Down
16 changes: 11 additions & 5 deletions app/resident_manager/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,18 @@ String? roomValidator(BuildContext context, {required bool required, required St
return null;
}

String? phoneValidator(BuildContext context, {required String? value}) {
if (value != null && value.isNotEmpty) {
final pattern = RegExp(r"^\+?[\d\s]+$");
if (value.length > 15 || !pattern.hasMatch(value)) {
return AppLocale.InvalidPhoneNumber.getString(context);
String? phoneValidator(BuildContext context, {required bool required, required String? value}) {
if (value == null || value.isEmpty) {
if (required) {
return AppLocale.MissingRequiredValue.getString(context);
}

return null;
}

final pattern = RegExp(r"^\+?[\d\s]+$");
if (value.length < 9 || value.length > 11 || !pattern.hasMatch(value)) {
return AppLocale.InvalidPhoneNumber.getString(context);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion app/resident_manager/lib/src/widgets/admin/reg_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class _RegisterQueuePageState extends AbstractCommonState<RegisterQueuePage> wit
DataCell(Text(r.name)),
DataCell(Text(r.room.toString())),
DataCell(Text(r.birthday?.format("dd/mm/yyyy") ?? "---")),
DataCell(Text(r.phone ?? "---")),
DataCell(Text(r.phone)),
DataCell(Text(r.email ?? "---")),
DataCell(Text(formatDateTime(r.createdAt.toLocal()))),
DataCell(Text(r.username ?? "---")),
Expand Down
5 changes: 3 additions & 2 deletions app/resident_manager/lib/src/widgets/admin/residents.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ class _EditButton extends StatelessWidget {
contentPadding: const EdgeInsets.all(8.0),
label: FieldLabel(
AppLocale.Phone.getString(context),
required: true,
style: const TextStyle(color: Colors.black),
),
),
validator: (value) => phoneValidator(context, value: value),
validator: (value) => phoneValidator(context, required: true, value: value),
),
TextFormField(
controller: emailController,
Expand Down Expand Up @@ -512,7 +513,7 @@ class _ResidentsPageState extends AbstractCommonState<ResidentsPage> with Common
DataCell(Text(r.name)),
DataCell(Text(r.room.toString())),
DataCell(Text(r.birthday?.format("dd/mm/yyyy") ?? "---")),
DataCell(Text(r.phone ?? "---")),
DataCell(Text(r.phone)),
DataCell(Text(r.email ?? "---")),
DataCell(Text(formatDateTime(r.createdAt.toLocal()))),
DataCell(Text(r.username ?? "---")),
Expand Down
3 changes: 2 additions & 1 deletion app/resident_manager/lib/src/widgets/personal_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ class _PersonalInfoPageState extends AbstractCommonState<PersonalInfoPage> with
contentPadding: const EdgeInsets.all(8.0),
label: FieldLabel(
AppLocale.Phone.getString(context),
required: true,
style: const TextStyle(color: Colors.black),
),
),
validator: (value) => phoneValidator(context, value: value),
validator: (value) => phoneValidator(context, required: true, value: value),
),
),
_InfoCard(
Expand Down
3 changes: 2 additions & 1 deletion app/resident_manager/lib/src/widgets/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ class _RegisterPageState extends AbstractCommonState<RegisterPage> with CommonSc
iconColor: Colors.white,
label: FieldLabel(
AppLocale.Phone.getString(context),
required: true,
style: const TextStyle(color: Colors.white),
),
),
style: const TextStyle(color: Colors.white),
validator: (value) => phoneValidator(context, value: value),
validator: (value) => phoneValidator(context, required: true, value: value),
),
TextFormField(
controller: _email,
Expand Down
2 changes: 1 addition & 1 deletion scripts/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'accounts' AND type = 'U')
name NVARCHAR(255) COLLATE Vietnamese_100_CS_AS_KS_WS NOT NULL,
room SMALLINT NOT NULL,
birthday DATE,
phone NVARCHAR(15),
phone NVARCHAR(15) NOT NULL,
email NVARCHAR(255),
username NVARCHAR(255) UNIQUE NOT NULL,
hashed_password NVARCHAR(255) NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def validate_room(room: int) -> bool:


def validate_phone(phone: str) -> bool:
return phone.isdigit() and len(phone) < 16
return phone.isdigit() and len(phone) in (9, 10, 11)


def validate_email(email: str) -> bool:
Expand Down
7 changes: 2 additions & 5 deletions server/v1/models/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PersonalInfo(pydantic.BaseModel):
name: Annotated[str, pydantic.Field(description="The full name of the resident")]
room: Annotated[int, pydantic.Field(description="The room number of the resident")]
birthday: Annotated[Optional[date], pydantic.Field(description="The resident's date of birth")] = None
phone: Annotated[Optional[str], pydantic.Field(description="The resident's phone number")] = None
phone: Annotated[str, pydantic.Field(description="The resident's phone number")]
email: Annotated[Optional[str], pydantic.Field(description="The resident's email")] = None

def to_personal_info(self) -> PersonalInfo:
Expand All @@ -37,9 +37,6 @@ def to_personal_info(self) -> PersonalInfo:
)

def validate_info(self) -> Optional[Result[None]]:
if self.phone is None or len(self.phone) == 0:
self.phone = None

if self.email is None or len(self.email) == 0:
self.email = None

Expand All @@ -49,7 +46,7 @@ def validate_info(self) -> Optional[Result[None]]:
if not validate_room(self.room):
return Result(code=102, data=None)

if self.phone is not None and not validate_phone(self.phone):
if not validate_phone(self.phone):
return Result(code=103, data=None)

if self.email is not None and not validate_email(self.email):
Expand Down

0 comments on commit d189b8a

Please sign in to comment.