-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from ladnishad/development
API v4.5
- Loading branch information
Showing
23 changed files
with
5,991 additions
and
1,281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,194 @@ | ||
import { Aircraft } from "../models/AircraftsModel" | ||
import { get as airportGetters } from "../controllers/airports/helpers" | ||
import { asyncMap } from "../helpers" | ||
import { Aircraft } from "../models/AircraftsModel"; | ||
import { get as airportGetters } from "../controllers/airports/helpers"; | ||
import { asyncMap } from "../helpers"; | ||
|
||
export const AircraftAggregations = { | ||
"AircraftAllDetailsAggregation": async({ aircraftId }) => { | ||
|
||
AircraftAllDetailsAggregation: async ({ aircraftId }) => { | ||
const pipeline = [ | ||
{ | ||
'$match': { | ||
'_id': aircraftId | ||
} | ||
$match: { | ||
_id: aircraftId, | ||
}, | ||
}, | ||
{ | ||
'$project': { | ||
'_id': 1, | ||
'registrationNum': 1, | ||
'aircraftTypeId': 1, | ||
'airlineId': 1, | ||
} | ||
$project: { | ||
_id: 1, | ||
registrationNum: 1, | ||
aircraftTypeId: 1, | ||
airlineId: 1, | ||
}, | ||
}, | ||
{ | ||
'$lookup': { | ||
'from': 'aircrafttypes', | ||
'localField': 'aircraftTypeId', | ||
'foreignField': '_id', | ||
'as': 'aircraftType' | ||
} | ||
$lookup: { | ||
from: "aircrafttypes", | ||
localField: "aircraftTypeId", | ||
foreignField: "_id", | ||
as: "aircraftType", | ||
}, | ||
}, | ||
{ | ||
'$unwind': { | ||
'path': '$aircraftType' | ||
} | ||
$unwind: { | ||
path: "$aircraftType", | ||
}, | ||
}, | ||
{ | ||
'$lookup': { | ||
'from': 'airlines', | ||
'localField': 'airlineId', | ||
'foreignField': '_id', | ||
'as': 'airline' | ||
} | ||
$lookup: { | ||
from: "airlines", | ||
localField: "airlineId", | ||
foreignField: "_id", | ||
as: "airline", | ||
}, | ||
}, | ||
{ | ||
'$unwind': { | ||
'path': '$airline' | ||
} | ||
$unwind: { | ||
path: "$airline", | ||
}, | ||
}, | ||
{ | ||
'$project': { | ||
'_id': 1, | ||
'registrationNum': 1, | ||
'aircraftType': 1, | ||
'airline': 1 | ||
} | ||
} | ||
] | ||
$project: { | ||
_id: 1, | ||
registrationNum: 1, | ||
aircraftType: 1, | ||
airline: 1, | ||
}, | ||
}, | ||
]; | ||
|
||
try{ | ||
const result = await Aircraft.aggregate(pipeline) | ||
return result | ||
} catch(e){ | ||
return e | ||
try { | ||
const result = await Aircraft.aggregate(pipeline); | ||
return result; | ||
} catch (e) { | ||
return e; | ||
} | ||
}, | ||
|
||
"getAllUserFlightsByAircrafts": async ({ userId }) => { | ||
getAllUserFlightsByAircrafts: async ({ userId }) => { | ||
const pipeline = [ | ||
{ | ||
'$lookup': { | ||
'from': 'aircrafttypes', | ||
'localField': 'aircraftTypeId', | ||
'foreignField': '_id', | ||
'as': 'aircraftType' | ||
} | ||
}, { | ||
'$unwind': { | ||
'path': '$aircraftType', | ||
'preserveNullAndEmptyArrays': true | ||
} | ||
}, { | ||
'$lookup': { | ||
'from': 'airlines', | ||
'localField': 'airlineId', | ||
'foreignField': '_id', | ||
'as': 'airline' | ||
} | ||
}, { | ||
'$unwind': { | ||
'path': '$airline', | ||
'preserveNullAndEmptyArrays': true | ||
} | ||
}, { | ||
'$lookup': { | ||
'from': 'flights', | ||
'localField': '_id', | ||
'foreignField': 'aircraftId', | ||
'as': 'flights' | ||
} | ||
}, { | ||
'$project': { | ||
'registrationNum': 1, | ||
'aircraftType': 1, | ||
'airline': 1, | ||
'flights': { | ||
'$filter': { | ||
'input': '$flights', | ||
'as': 'flight', | ||
'cond': { | ||
'$eq': [ | ||
'$$flight.userId', userId | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
$lookup: { | ||
from: "aircrafttypes", | ||
localField: "aircraftTypeId", | ||
foreignField: "_id", | ||
as: "aircraftType", | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$aircraftType", | ||
preserveNullAndEmptyArrays: true, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "airlines", | ||
localField: "airlineId", | ||
foreignField: "_id", | ||
as: "airline", | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$airline", | ||
preserveNullAndEmptyArrays: true, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "flights", | ||
localField: "_id", | ||
foreignField: "aircraftId", | ||
as: "flights", | ||
}, | ||
}, | ||
{ | ||
$project: { | ||
registrationNum: 1, | ||
aircraftType: 1, | ||
airline: 1, | ||
flights: { | ||
$filter: { | ||
input: "$flights", | ||
as: "flight", | ||
cond: { | ||
$eq: ["$$flight.userId", userId], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
flights: { | ||
$ne: [], | ||
}, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "images", | ||
localField: "_id", | ||
foreignField: "aircraftId", | ||
as: "aircraftImages", | ||
}, | ||
}, | ||
]; | ||
|
||
try{ | ||
const aggregateResult = await Aircraft.aggregate(pipeline) | ||
try { | ||
const aggregateResult = await Aircraft.aggregate(pipeline); | ||
|
||
const processedResult = await asyncMap(aggregateResult, async(eachResult) => { | ||
let resultFlights = eachResult.flights | ||
const processedResult = await asyncMap( | ||
aggregateResult, | ||
async (eachResult) => { | ||
let resultFlights = eachResult.flights; | ||
|
||
resultFlights = await asyncMap(resultFlights, async({ _id, userId, flightNumber, flightDate, flightOriginAirportId, flightDestinationAirportId, caption, visibility }) => { | ||
let originAirport = {} | ||
let destinationAirport = {} | ||
resultFlights = await asyncMap( | ||
resultFlights, | ||
async ({ | ||
_id, | ||
userId, | ||
flightNumber, | ||
flightDate, | ||
flightOriginAirportId, | ||
flightDestinationAirportId, | ||
caption, | ||
visibility, | ||
}) => { | ||
let originAirport = {}; | ||
let destinationAirport = {}; | ||
|
||
if(flightOriginAirportId){ | ||
originAirport = await airportGetters.airportById({ airportId: flightOriginAirportId }) | ||
} | ||
if (flightOriginAirportId) { | ||
originAirport = await airportGetters.airportById({ | ||
airportId: flightOriginAirportId, | ||
}); | ||
} | ||
|
||
if(flightDestinationAirportId){ | ||
destinationAirport = await airportGetters.airportById({ airportId: flightDestinationAirportId }) | ||
} | ||
if (flightDestinationAirportId) { | ||
destinationAirport = await airportGetters.airportById({ | ||
airportId: flightDestinationAirportId, | ||
}); | ||
} | ||
|
||
return { | ||
flightId: _id, | ||
userId, | ||
flightNumber, | ||
flightDate, | ||
originAirport, | ||
destinationAirport, | ||
caption, | ||
visibility | ||
} | ||
}) | ||
return { | ||
flightId: _id, | ||
userId, | ||
flightNumber, | ||
flightDate, | ||
originAirport, | ||
destinationAirport, | ||
caption, | ||
visibility, | ||
}; | ||
} | ||
); | ||
|
||
eachResult.flights = resultFlights | ||
return eachResult | ||
}) | ||
eachResult.flights = resultFlights; | ||
return eachResult; | ||
} | ||
); | ||
|
||
return processedResult | ||
} catch(e){ | ||
return e | ||
return processedResult; | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Notification } from "../models/NotificationsModel"; | ||
|
||
export const NotificationAggregations = { | ||
getUserNotifications: async ({ userId }) => { | ||
const pipeline = [ | ||
{ | ||
$match: { | ||
impactUserIds: userId, | ||
}, | ||
}, | ||
{ | ||
$lookup: { | ||
from: "users", | ||
localField: "actorUserId", | ||
foreignField: "_id", | ||
as: "actor", | ||
}, | ||
}, | ||
{ | ||
$unwind: { | ||
path: "$actor", | ||
}, | ||
}, | ||
{ | ||
$sort: { | ||
timestamp: -1, | ||
}, | ||
}, | ||
]; | ||
|
||
try { | ||
const result = await Notification.aggregate(pipeline); | ||
return result; | ||
} catch (e) { | ||
return e; | ||
} | ||
}, | ||
}; |
Oops, something went wrong.