@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
215 lines • 8.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirestoreModelGeoValueConverter = exports.ModelGeoValueConverter = void 0;
const model_field_value_converter_1 = require("../model_field_value_converter");
const utils_1 = require("../../utils");
const firestore_1 = require("firebase-admin/firestore");
const model_field_value_1 = require("../model_field_value");
/**
* ModelGeoValue ModelFieldValueConverter.
*
* ModelGeoValue用のModelFieldValueConverter。
*/
class ModelGeoValueConverter extends model_field_value_converter_1.ModelFieldValueConverter {
/**
* ModelGeoValue ModelFieldValueConverter.
*
* ModelGeoValue用のModelFieldValueConverter。
*/
constructor() {
super();
}
type = "ModelGeoValue";
convertFrom(key, value, original) {
if (value !== null && typeof value === "object" && "@type" in value && value["@type"] === this.type) {
const latitude = value["@latitude"] ?? 0;
const longitude = value["@longitude"] ?? 0;
return {
[key]: new model_field_value_1.ModelGeoValue(latitude, longitude, "server"),
};
}
return null;
}
convertTo(key, value, original) {
if (value instanceof model_field_value_1.ModelGeoValue) {
return {
[key]: {
"@type": this.type,
"@latitude": value["@latitude"],
"@longitude": value["@longitude"],
"@source": value["@source"],
},
};
}
return null;
}
}
exports.ModelGeoValueConverter = ModelGeoValueConverter;
/**
* FirestoreConverter for [ModelGeoValue].
*
* [ModelGeoValue]用のFirestoreConverter。
*/
class FirestoreModelGeoValueConverter extends model_field_value_converter_1.FirestoreModelFieldValueConverter {
/**
* FirestoreConverter for [ModelGeoValue].
*
* [ModelGeoValue]用のFirestoreConverter。
*/
constructor() {
super();
}
type = "ModelGeoValue";
convertFrom(key, value, original, firestoreInstance) {
if (typeof value === "string" || value instanceof firestore_1.GeoPoint) {
const targetKey = `#${key}`;
const targetMap = original[targetKey] ?? {};
const type = targetMap["@type"] ?? "";
if (type == this.type) {
return {
[key]: {
"@type": this.type,
"@latitude": targetMap["@latitude"] ?? 0,
"@longitude": targetMap["@longitude"] ?? 0,
},
[targetKey]: null,
};
}
}
else if (Array.isArray(value)) {
const targetKey = `#${key}`;
const targetList = original[targetKey] ?? [];
if (targetList != null && targetList.length > 0 && targetList.every((e) => e["@type"] === this.type)) {
const res = [];
for (const tmp of targetList) {
res.push({
"@type": this.type,
"@latitude": tmp["@latitude"] ?? 0,
"@longitude": tmp["@longitude"] ?? 0,
});
}
return {
[key]: res,
[targetKey]: null,
};
}
}
else if ((0, utils_1.isDynamicMap)(value)) {
const targetKey = `#${key}`;
const targetMap = original[targetKey] ?? {};
targetMap;
if (targetMap != null) {
const res = {};
for (const key in value) {
const val = value[key];
const mapVal = targetMap[key] ?? {};
const type = mapVal["@type"] ?? "";
if (type != this.type) {
continue;
}
if (typeof val === "string" || val instanceof firestore_1.GeoPoint) {
res[key] = {
"@type": this.type,
"@latitude": mapVal["@latitude"] ?? 0,
"@longitude": mapVal["@longitude"] ?? 0,
};
}
}
if (Object.keys(res).length > 0) {
return {
[targetKey]: null,
[key]: res,
};
}
}
}
return null;
}
convertTo(key, value, _original, firestoreInstance) {
if (value != null && typeof value === "object" && "@type" in value) {
const type = value["@type"] ?? "";
if (type === this.type) {
const fromUser = value["@source"] === "user";
const geoHash = value["@geoHash"] ?? "";
const latitude = value["@latitude"] ?? 0;
const longitude = value["@longitude"] ?? 0;
const targetKey = `#${key}`;
const result = {
[targetKey]: {
"@type": type,
"@latitude": latitude,
"@longitude": longitude,
"@target": key,
},
};
if (fromUser) {
result[key] = geoHash;
}
return result;
}
}
else if (Array.isArray(value)) {
const list = value.filter((e) => e != null && typeof e === "object" && "@type" in e);
if (list.length > 0 && list.every((e) => e["@type"] === this.type)) {
const target = [];
const res = [];
const targetKey = `#${key}`;
for (const entry of list) {
const fromUser = entry["@source"] === "user";
const geoHash = entry["@geoHash"] ?? "";
const latitude = entry["@latitude"] ?? 0;
const longitude = entry["@longitude"] ?? 0;
target.push({
"@type": this.type,
"@latitude": latitude,
"@longitude": longitude,
"@target": key,
});
if (fromUser) {
res.push(geoHash);
}
}
return {
[targetKey]: target,
[key]: res,
};
}
}
else if ((0, utils_1.isDynamicMap)(value)) {
const map = {};
for (const k in value) {
const v = value[k];
if (v != null && typeof v === "object" && "@type" in v) {
map[k] = v;
}
}
if (Object.keys(map).length > 0 && Object.values(map).every((e) => e["@type"] === this.type)) {
const target = {};
const res = {};
const targetKey = `#${key}`;
for (const [k, entry] of Object.entries(map)) {
const fromUser = entry["@source"] === "user";
const geoHash = entry["@geoHash"] ?? "";
const latitude = entry["@latitude"] ?? 0;
const longitude = entry["@longitude"] ?? 0;
target[k] = {
"@type": this.type,
"@latitude": latitude,
"@longitude": longitude,
"@target": key,
};
if (fromUser) {
res[k] = geoHash;
}
}
return {
[targetKey]: target,
[key]: res,
};
}
}
return null;
}
}
exports.FirestoreModelGeoValueConverter = FirestoreModelGeoValueConverter;
//# sourceMappingURL=model_geo_value_converter.js.map