mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim-api.git
synced 2026-08-20 21:14:15 +00:00
changed the listing function on contacts frontend, still got issues
This commit is contained in:
@@ -11,32 +11,51 @@ class ContactController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
//return Auth::user()->company()->first()->id;
|
||||
$output= array();
|
||||
$contacts = Contact::where('user_id', Auth::user()->id)->get();
|
||||
$companies= Contact::with('companies')->get();
|
||||
//$companies =\DB::table('companies')->select('contacts.com_id', 'companies.id', 'companies.company_name')->join('contacts', 'contacts.com_id', '=', 'companies.id')->get();
|
||||
//$companies = "SELECT contacts.com_id, companies.id, companies.company_name FROM `companies` INNER JOIN contacts ON contacts.com_id = companies.id";
|
||||
|
||||
// $companies = \DB::table('companies')
|
||||
// ->select('id', 'company_name')
|
||||
// ->where('com_id', Auth::user()->id)
|
||||
// ->get();
|
||||
// $contacts = Contact::where('sender_id', Auth::user()->id)->get();
|
||||
//$companies= Contact::with('companies')->get();
|
||||
$contacts =\DB::table('contacts')->select('contacts.id as contact_id', 'sender_company.company_name as sender_company_name', 'sender_company.id as sender_company_id', 'recipient_company.id as recipient_company_id', 'recipient_company.company_name as recipient_company_name', 'contacts.status', 'sender_company.tel_no as sender_tel_no', 'recipient_company.tel_no as recipient_tel_no', 'sender_company.contact_person as sender_contact_pers', 'recipient_company.contact_person as recipient_contact_pers')->leftJoin('companies as sender_company', 'sender_company.id', '=', 'contacts.sender_id')->leftJoin('companies as recipient_company', 'recipient_company.id', '=', 'contacts.recipient_id')->where('contacts.sender_id', Auth::user()->company()->first()->id)->orWhere('contacts.recipient_id', Auth::user()->company()->first()->id)->get();
|
||||
|
||||
if (!$contacts)
|
||||
{
|
||||
return response()->json(['message'=>'Access Denied!'], 404);
|
||||
}
|
||||
|
||||
return response()->json($companies, 200);
|
||||
$contacts2 = $contacts->toArray();
|
||||
// $toprint = $contacts2[0]->sender_company_name;
|
||||
// echo $toprint;
|
||||
$length = count($contacts2);
|
||||
|
||||
for ($i=0; $i < $length; $i++) {
|
||||
# code...
|
||||
if($contacts2[$i]->sender_company_id == Auth::user()->company()->first()->id){
|
||||
$contacts2[$i]->is_me = 1;
|
||||
}
|
||||
else{
|
||||
$contacts2[$i]->is_me = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// foreach ($contacts2 as $contact => $value) {
|
||||
// print_r($value->sender_company_name);
|
||||
// //if($value['sender_company_id'] == Auth::user()->company()->first()->id){
|
||||
// // var_dump($contact);
|
||||
// //print_r($value['sender_company_id']);
|
||||
// //}
|
||||
|
||||
// //$contact->helo
|
||||
// }
|
||||
return response()->json($contacts2, 200);
|
||||
}
|
||||
|
||||
public function sendRequest(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$com_id = $request->input('com_id');
|
||||
$recipient_id = $request->input('recipient_id');
|
||||
|
||||
// query to find company asisng to $company
|
||||
$company = Company::find($com_id);
|
||||
$company = Company::find($recipient_id);
|
||||
|
||||
// get user from session
|
||||
$request->user();
|
||||
@@ -44,51 +63,48 @@ class ContactController extends Controller
|
||||
|
||||
// prepare query
|
||||
$contact = new Contact();
|
||||
$contact->com_id = $company->id;
|
||||
$contact->user_id = $user->id;
|
||||
$contact->recipient_id = $company->id;
|
||||
$contact->sender_id = $user->id;
|
||||
$contact->save();
|
||||
|
||||
return response()->json($contact, 201);
|
||||
}
|
||||
|
||||
public function acceptRequest(Request $request, $id)
|
||||
public function acceptRequest(Request $request)
|
||||
{
|
||||
$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
|
||||
$contact = Contact::where('id', $request->input('id'))->first();//->where('id', $id)->first();
|
||||
//$contact = Contact::where('id', '=', e($id))->first();
|
||||
if($contact)
|
||||
{
|
||||
$action=$request->input('action');
|
||||
if ($action==1)
|
||||
{
|
||||
$contact->status=1;
|
||||
}
|
||||
if ($action==0)
|
||||
{
|
||||
$contact->status=0;
|
||||
}
|
||||
else{
|
||||
|
||||
return response()->json(["message" => "not allowed"], 400);
|
||||
}
|
||||
$contact->status=1;
|
||||
|
||||
$saved = $contact->save();
|
||||
if ($saved){
|
||||
return response()->json($contact, 201);
|
||||
return response()->json(["success"=> true, "message" => "Successfully accepted"], 201);
|
||||
}
|
||||
return response()->json($contact, 400);
|
||||
else{
|
||||
return response()->json($contact, ["message" => "not saved"], 400);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return response()->json( ["message" => "not allowed"], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
|
||||
$contact = Contact::where('id', $request->input('id'))->first();
|
||||
//$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
|
||||
|
||||
if (!$contact)
|
||||
{
|
||||
return response()->json(['message'=>'Access Denied!'], 404);
|
||||
}
|
||||
|
||||
$contact->delete($id);
|
||||
else
|
||||
{
|
||||
$contact->delete();
|
||||
}
|
||||
|
||||
return response()->json($contact, 204);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user