Skip to content

Commit

Permalink
Merge pull request #34 from ladnishad/development
Browse files Browse the repository at this point in the history
API v4.5
  • Loading branch information
ladnishad authored Jan 22, 2023
2 parents a8a532f + 0590ccb commit 59ef0a2
Show file tree
Hide file tree
Showing 23 changed files with 5,991 additions and 1,281 deletions.
290 changes: 163 additions & 127 deletions aggregations/AircraftAggregations.js
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;
}
}
}
},
};
32 changes: 32 additions & 0 deletions aggregations/FlightAggregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ export const FlightAggregations = {
}
]

try{
const result = await Flight.aggregate(pipeline)
return result
} catch(e){
return e
}
},
"flights.userRepeatedAircraftFlights": async ({ userId }) => {
const pipeline = [
{
'$match': {
'userId': userid
}
}, {
'$group': {
'_id': '$aircraftId',
'flights': {
'$push': '$$ROOT'
},
'count': {
'$sum': 1
}
}
}, {
'$match': {
'count': {
'$gt': 1
}
}
}
]

try{
const result = await Flight.aggregate(pipeline)
return result
Expand Down
38 changes: 38 additions & 0 deletions aggregations/NotificationAggregations.js
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;
}
},
};
Loading

0 comments on commit 59ef0a2

Please sign in to comment.