Skip to content

Commit

Permalink
Merge pull request #206 from dfpc-coe/MissionDiffLayer
Browse files Browse the repository at this point in the history
Diff on MissionLayer
  • Loading branch information
ingalls authored Jun 21, 2024
2 parents 0d8c206 + fd60c6b commit 6d3bb84
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
56 changes: 53 additions & 3 deletions api/lib/api/mission-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import TAKAPI from '../tak-api.js';
import { TAKList } from './types.js';
import { Type, Static } from '@sinclair/typebox';
import { MissionOptions } from './mission.js';
import Err from '@openaddresses/batch-error';
import { Feature } from '@tak-ps/node-cot';

export enum MissionLayerType {
GROUP = 'GROUP',
Expand All @@ -18,10 +20,21 @@ export const MissionLayer = Type.Object({
uid: Type.String(),
mission_layers: Type.Array(Type.Any()),
uids: Type.Array(Type.Object({
data: Type.String(),
data: Type.String({
description: 'The UID of the COT'
}),
timestamp: Type.String(),
creatorUid: Type.String(),
keywords: Type.Array(Type.String())
keywords: Type.Optional(Type.Array(Type.String())),
detail: Type.Optional(Type.Object({
type: Type.String(),
callsign: Type.String(),
color: Type.String(),
location: Type.Object({
lat: Type.Number(),
lon: Type.Number()
})
}))
})),
contents: Type.Array(Type.Any()),
maplayers: Type.Array(Type.Any())
Expand Down Expand Up @@ -77,7 +90,7 @@ export default class {
method: 'GET',
headers: this.#headers(opts),
});

res.data.map((l) => {
if (l.type === MissionLayerType.UID && !l.uids) {
l.uids = [];
Expand All @@ -88,6 +101,43 @@ export default class {
return res;
}

/**
* Stopgap function until the main latestFeats function can accept a path
* parameter
*/
async latestFeats(
name: string,
layerUid: string, // Layer UID
opts?: Static<typeof MissionOptions>
): Promise<Static<typeof Feature>[]> {
const layer = await this.get(name, layerUid, opts);
const feats = await this.api.Mission.latestFeats(name, opts);

const layerUids = new Set(layer.uids.map((u) => {
return u.data
}));

return feats.filter((f) => {
return layerUids.has(f.id)
});
}

async get(
name: string,
layerUid: string, // Layer UID
opts?: Static<typeof MissionOptions>
): Promise<Static<typeof MissionLayer>> {
const layers = await this.list(name, opts);

for (const layer of layers.data) {
if (layer.uid === layerUid) {
return layer;
}
}

throw new Err(404, null, `Layer ${layerUid} not found`);
}

async create(
name: string,
query: Static<typeof CreateInput>,
Expand Down
11 changes: 9 additions & 2 deletions api/routes/connection-layer-cot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ export default async function router(schema: Schema, config: Config) {
pooledClient = await config.conns.get(data.connection);

if (data.mission_diff) {
if (!Array.isArray(req.body.uids)) throw new Err(400, null, 'uids Array must be present when submitting to DataSync with MissionDiff');
if (!Array.isArray(req.body.uids)) {
throw new Err(400, null, 'uids Array must be present when submitting to DataSync with MissionDiff');
}

const api = await TAKAPI.init(new URL(String(config.server.api)), new APIAuthCertificate(pooledClient.config.auth.cert, pooledClient.config.auth.key));
// Once NodeJS supports Set.difference we can simplify this
const inputFeats = new Set(req.body.uids);
const features = await api.Mission.latestFeats(data.name, { token: data.mission_token });

const features = await api.MissionLayer.latestFeats(
data.name,
`layer-${layer.id}`,
{ token: data.mission_token }
);

for (const feat of features.values()) {
if (!inputFeats.has(String(feat.id))) {
Expand Down

0 comments on commit 6d3bb84

Please sign in to comment.