Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code structure changes. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/Http/Controllers/Api/V1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct()
)]
public function signUp(SignUpRequest $request): JsonResponse
{
$data = $this->authService->signup($request->all());
$data = $this->authService->signup($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ enum: ['verification', 'reset_password', 'update_profile'],
)]
public function sendOtp(SendOtpRequest $request): JsonResponse
{
$data = $this->authService->sendOtp($request->all());
$data = $this->authService->sendOtp($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public function sendOtp(SendOtpRequest $request): JsonResponse
)]
public function verifyOtp(VerifyOtpRequest $request): JsonResponse
{
$data = $this->authService->verifyOtp($request->all());
$data = $this->authService->verifyOtp($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public function verifyOtp(VerifyOtpRequest $request): JsonResponse
)]
public function login(LoginRequest $request)
{
$data = $this->authService->login($request->all());
$data = $this->authService->login($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public function forgetPassword(ForgetPasswordRequest $request): JsonResponse
)]
public function resetPasswordOtp(ResetPasswordOtpRequest $request): JsonResponse
{
$data = $this->authService->resetPasswordOtp($request->all());
$data = $this->authService->resetPasswordOtp($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -445,7 +445,7 @@ public function resetPasswordOtp(ResetPasswordOtpRequest $request): JsonResponse
)]
public function resetPassword(ResetPasswordRequest $request): JsonResponse
{
$data = $this->authService->resetPassword($request->all());
$data = $this->authService->resetPassword($request->validated());

return $this->success($data, 200);
}
Expand Down Expand Up @@ -502,7 +502,7 @@ public function resetPassword(ResetPasswordRequest $request): JsonResponse
)]
public function changePassword(ChangePasswordRequest $request): JsonResponse
{
$data = $this->authService->changePassword($request->all());
$data = $this->authService->changePassword($request->validated());

return $this->success($data, 200);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/V1/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ public function show(string $language): JsonResponse
{
$data = $this->langService->resource($language);

return isset($data['errors']) ? $this->error($data) : $this->success($data, 200);
return $this->success($data, 200);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/V1/MasterSettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function index(Request $request)
}

#[OA\Get(
path: '/api/v1/versions/{masterSetting_id}',
path: '/api/v1/settings/{masterSetting_id}',
operationId: 'getVersionDetail',
tags: ['MasterSettings'],
summary: 'Get detail of settings',
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/V1/SignedUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function __construct()
)]
public function __invoke(SignedUrlRequest $request)
{
$signedUrlObj = $this->signedUrlService->create($request->all());
$signedUrlObj = $this->signedUrlService->create($request->validated());

return isset($signedUrlObj['errors']) ? $this->error($signedUrlObj) : $this->success($signedUrlObj, 200);
return $this->success($signedUrlObj, 200);
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/V1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public function me(): JsonResponse
)]
public function updateProfile(UpdateProfile $request): JsonResponse
{
$data = $this->userService->update(Auth::id(), $request->all());
$data = $this->userService->update(Auth::id(), $request->validated());

return isset($data['errors']) ? $this->error($data) : $this->success($data, 200);
return $this->success($data, 200);
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/Auth/ResetPasswordOtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function rules(): array
return [
'email' => 'required|email|max:255',
'password' => 'required|min:8|confirmed',
'otp' => 'required|digits:' . config('site.generateOtpLength'),
'otp' => 'required|digits:' . config('site.generate_otp_length'),
];
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/Auth/VerifyOtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function authorize(): bool

public function rules(): array
{
$otpLength = config('site.generateOtpLength');
$otpLength = config('site.generate_otp_length');

return [
'email' => 'required|email|max:120',
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/ForgetPasswordOtpMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function handle(): void
'otp' => $this->otp,
'firstname' => $this->user->first_name,
'lastname' => $this->user->last_name,
'subject' => __('email.forgetPasswordEmailSubject'),
];
Mail::to($this->user->email)->send(new ForgetPasswordOtp(data: $data));
} catch (\Exception $e) {
Expand Down
11 changes: 6 additions & 5 deletions app/Jobs/VerifyUserMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ class VerifyUserMail implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(private ?object $user, private int|string $otp) {}
public function __construct(private ?object $user, private int|string $otp)
{
}

/**
* Execute the job.
*/
public function handle(): void
{
Log::info($this->user);

Log::info($this->user->first_name);
try {
$data = [
'otp' => $this->otp,
'firstname' => $this->user->firstname,
'lastname' => $this->user->lastname,
'firstname' => $this->user->first_name,
'lastname' => $this->user->last_name,
'subject' => __('email.verifyUserSubject'),
];
Mail::to($this->user->email)->send(new VerifyUser($data));
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/ForgetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ForgetPassword extends Mailable
public function __construct(private ?array $data)
{
$this->url = url(
config('site.frontWebsiteUrl') .
config('site.front_website_url') .
'/reset-password?token=' .
$data['reset_password_token'] .
'&email=' .
Expand Down
3 changes: 2 additions & 1 deletion app/Mail/ForgetPasswordOtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Log;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Envelope;
Expand All @@ -23,7 +24,7 @@ public function __construct(private ?array $data)
public function envelope(): Envelope
{
return new Envelope(
subject: __('email.forgetPasswordEmailSubject'),
subject: $this->data['subject'],
);
}

Expand Down
11 changes: 10 additions & 1 deletion app/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Country extends Model
{
use BaseModel,HasFactory;
use BaseModel, HasFactory;

protected $fillable = [
'iso',
Expand All @@ -17,4 +17,13 @@ class Country extends Model
'numcode',
'phonecode',
];

protected function casts(): array
{
return [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
}
}
13 changes: 8 additions & 5 deletions app/Models/MasterSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class MasterSetting extends Model
'is_public', // If key is false, visible only for authenticated user. If true, visible for every user.
];

protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
protected function casts(): array
{
return [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
}

protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
}
12 changes: 4 additions & 8 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Traits\BaseModel;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Facades\Hash;
Expand Down Expand Up @@ -45,7 +43,7 @@ class User extends Authenticatable
'remember_token',
];

protected $dates = ['created_at'];
// protected $dates = ['created_at'];

protected $relationship = [];

Expand All @@ -60,11 +58,9 @@ protected function casts(): array
'email_verified_at' => 'datetime',
'password' => 'hashed',
'last_login_at' => 'datetime',
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
}

protected function password(): Attribute
{
return Attribute::set(fn (string $password) => Hash::make($password));
}
}
15 changes: 8 additions & 7 deletions app/Models/UserOtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ class UserOtp extends Model
],
];

protected $cast = [
'created_at' => 'timestamp',
];

protected $dates = [
'created_at',
];
protected function casts(): array
{
return [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
}

/**
* Model's relationships
Expand Down
24 changes: 11 additions & 13 deletions app/Services/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public function __construct()

public function signup(array $inputs): array
{
DB::beginTransaction();
$user = $this->userObj->create($inputs);
$otp = Helper::generateOTP(config('site.generateOtpLength'));
$otp = Helper::generateOTP(config('site.generate_otp_length'));
$this->userOtpService->store(['otp' => $otp, 'user_id' => $user->id, 'otp_for' => 'verification']);
DB::commit();

try {
VerifyUserMail::dispatch($user, $otp);
} catch (\Exception $e) {
Expand All @@ -59,13 +58,13 @@ public function signup(array $inputs): array

public function sendOtp(array $inputs): array
{
$user = $this->userObj->whereEmail($inputs['email'])->first();
$user = $this->userObj->where('email', $inputs['email'])->first();

if (empty($user)) {
throw new CustomException(__('message.emailNotExist'));
}

$otp = Helper::generateOTP(config('site.generateOtpLength'));
$otp = Helper::generateOTP(config('site.generate_otp_length'));

switch ($inputs['otp_for']) {
case 'verification':
Expand Down Expand Up @@ -99,7 +98,7 @@ public function sendOtp(array $inputs): array

public function verifyOtp(array $inputs): array
{
$user = $this->userObj->whereEmail($inputs['email'])->first();
$user = $this->userObj->where('email', $inputs['email'])->first();

if (empty($user)) {
throw new CustomException(__('message.emailNotExist'));
Expand All @@ -117,7 +116,7 @@ public function verifyOtp(array $inputs): array
}

$this->userOtpService->update($userOtp['id'], ['verified_at' => date('Y-m-d h:i:s')]);
$this->userObj->whereId($user['id'])->update(['email_verified_at' => date('Y-m-d h:i:s')]);
$this->userObj->where('id', $user['id'])->update(['email_verified_at' => date('Y-m-d h:i:s')]);

$data = [
'message' => __('message.userVerifySuccess'),
Expand All @@ -128,7 +127,7 @@ public function verifyOtp(array $inputs): array

public function login(array $inputs): array
{
$user = $this->userObj->whereEmail($inputs['email'])->first();
$user = $this->userObj->where('email', $inputs['email'])->first();

if (!$user || !Hash::check($inputs['password'], $user->password)) {
throw new CustomException(__('auth.failed'));
Expand All @@ -151,14 +150,14 @@ public function login(array $inputs): array

public function forgetPasswordOtp(array $inputs): array
{
$user = $this->userObj->whereEmail($inputs['email'])->first();
$user = $this->userObj->where('email', $inputs['email'])->first();
if (empty($user)) {
throw new CustomException(__('message.emailNotExist'));
}

$this->userOtpObj->whereUserId($user['id'])->where('otp_for', 'reset_password')->delete();
$this->userOtpObj->where('user_id', $user['id'])->where('otp_for', 'reset_password')->delete();

$otp = Helper::generateOTP(config('site.generateOtpLength'));
$otp = Helper::generateOTP(config('site.generate_otp_length'));
$this->userOtpService->store(['otp' => $otp, 'user_id' => $user->id, 'otp_for' => 'reset_password']);

try {
Expand Down Expand Up @@ -192,7 +191,7 @@ public function forgetPassword(array $inputs): array

public function resetPasswordOtp(array $inputs): array
{
$user = $this->userObj->whereEmail($inputs['email'])->first();
$user = $this->userObj->where('email', $inputs['email'])->first();

if (empty($user)) {
throw new CustomException(__('message.emailNotExist'));
Expand Down Expand Up @@ -227,7 +226,6 @@ public function resetPassword(array $inputs): array
'token' => $inputs['token'],
'email' => $inputs['email'],
'password' => $inputs['password'],
'password_confirmation' => $inputs['password_confirmation'],
], function (User $user, string $password) {
$user->forceFill([
'password' => $password,
Expand Down
2 changes: 1 addition & 1 deletion app/Services/UserOtpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function update(int $id, array $inputs): bool

public function isOtpExpired(int|string $createdAt, int|string|null $verifiedAt): string
{
$expirationTime = config('site.otpExpirationTimeInMinutes');
$expirationTime = config('site.otp_expiration_time_in_minutes');

$expirationDate = Carbon::parse($createdAt)->addMinutes($expirationTime)->format('Y-m-d H:i:s');

Expand Down
Loading