diff --git a/src/schema/api.graphql b/src/schema/api.graphql index ea194f33..f72c0fe4 100644 --- a/src/schema/api.graphql +++ b/src/schema/api.graphql @@ -243,6 +243,10 @@ type DeleteLabelPayload { label: Label } +type DeleteLocationPayload { + location: Location! +} + input DeleteSnapshotInput { deviceId: String! name: String! @@ -557,6 +561,7 @@ type Mutation { deleteBlueprint(id: String!): DeleteBlueprintPayload! deleteDevice(id: String!): DeleteDevicePayload! deleteLabel(id: String!): DeleteLabelPayload! + deleteLocation(id: String!): DeleteLocationPayload! deleteSnapshot(input: DeleteSnapshotInput!): DeleteSnapshotPayload deleteStream(id: String!): DeleteStreamPayload! importCSV(input: CSVImportInput!): CSVImport @@ -571,6 +576,7 @@ type Mutation { updateDevice(id: String!, input: UpdateDeviceInput!): UpdateDevicePayload! updateDiscoveredAt(deviceIds: [String!]!): [DeviceDiscoveryPayload!]! updateGraphNodeCoordinates(input: UpdateGraphNodeCoordinatesInput!): UpdateGraphNodeCoordinatesPayload! + updateLocation(id: String!, input: UpdateLocationInput!): UpdateLocationPayload! updateStream(id: String!, input: UpdateStreamInput!): UpdateStreamPayload! } @@ -940,6 +946,16 @@ type UpdateGraphNodeCoordinatesPayload { deviceNames: [String!]! } +input UpdateLocationInput { + coordinates: Coordinates! + countryId: String + name: String! +} + +type UpdateLocationPayload { + location: Location! +} + input UpdateStreamInput { blueprintId: String deviceName: String! diff --git a/src/schema/location.ts b/src/schema/location.ts index de19b0ec..818849ea 100644 --- a/src/schema/location.ts +++ b/src/schema/location.ts @@ -1,8 +1,8 @@ import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'; import { connectionFromArray } from 'graphql-relay'; import countries from 'i18n-iso-countries'; -import { arg, extendType, inputObjectType, nonNull, objectType } from 'nexus'; -import { toGraphId } from '../helpers/id-helper'; +import { arg, extendType, inputObjectType, nonNull, objectType, stringArg } from 'nexus'; +import { fromGraphId, toGraphId } from '../helpers/id-helper'; import { Node, PageInfo, PaginationConnectionArgs } from './global-types'; import { getCountryName } from '../helpers/location-helpers'; @@ -165,3 +165,82 @@ export const AddLocationMutation = extendType({ }); }, }); + +export const UpdateLocationInput = inputObjectType({ + name: 'UpdateLocationInput', + definition: (t) => { + t.nonNull.string('name'); + t.string('countryId'); + t.nonNull.field({ name: 'coordinates', type: Coordinates }); + }, +}); + +export const UpdateLocationPayload = objectType({ + name: 'UpdateLocationPayload', + definition: (t) => { + t.nonNull.field('location', { type: Location }); + }, +}); + +export const UpdateLocationMutation = extendType({ + type: 'Mutation', + definition: (t) => { + t.nonNull.field('updateLocation', { + type: UpdateLocationPayload, + args: { + id: nonNull(stringArg()), + input: nonNull(arg({ type: UpdateLocationInput })), + }, + resolve: async (_, args, { prisma, tenantId }) => { + const nativeId = fromGraphId('Location', args.id); + const { input } = args; + + const countryName = getCountryName(input.countryId ?? null); + + const location = await prisma.location.update({ + where: { id: nativeId }, + data: { + tenantId, + name: input.name, + country: countryName, + latitude: input.coordinates.latitude.toString(), + longitude: input.coordinates.latitude.toString(), + }, + }); + return { + location, + }; + }, + }); + }, +}); + +export const DeleteLocationPayload = objectType({ + name: 'DeleteLocationPayload', + definition: (t) => { + t.nonNull.field('location', { type: Location }); + }, +}); + +export const DeleteLocationMutation = extendType({ + type: 'Mutation', + definition: (t) => { + t.nonNull.field('deleteLocation', { + type: DeleteLocationPayload, + args: { + id: nonNull(stringArg()), + }, + resolve: async (_, args, { prisma, tenantId }) => { + const nativeId = fromGraphId('Location', args.id); + + const location = await prisma.location.delete({ + where: { id: nativeId, AND: { tenantId } }, + }); + + return { + location, + }; + }, + }); + }, +}); diff --git a/src/schema/nexus-typegen.ts b/src/schema/nexus-typegen.ts index b00f8250..f8d95852 100644 --- a/src/schema/nexus-typegen.ts +++ b/src/schema/nexus-typegen.ts @@ -193,6 +193,12 @@ export interface NexusGenInputs { coordinates: NexusGenInputs['GraphNodeCoordinatesInput'][]; // [GraphNodeCoordinatesInput!]! layer?: NexusGenEnums['TopologyLayer'] | null; // TopologyLayer }; + UpdateLocationInput: { + // input type + coordinates: NexusGenInputs['Coordinates']; // Coordinates! + countryId?: string | null; // String + name: string; // String! + }; UpdateStreamInput: { // input type blueprintId?: string | null; // String @@ -355,6 +361,10 @@ export interface NexusGenObjects { // root type label?: NexusGenRootTypes['Label'] | null; // Label }; + DeleteLocationPayload: { + // root type + location: NexusGenRootTypes['Location']; // Location! + }; DeleteSnapshotPayload: { // root type snapshot?: NexusGenRootTypes['Snapshot'] | null; // Snapshot @@ -783,6 +793,10 @@ export interface NexusGenObjects { // root type deviceNames: string[]; // [String!]! }; + UpdateLocationPayload: { + // root type + location: NexusGenRootTypes['Location']; // Location! + }; UpdateStreamPayload: { // root type stream?: NexusGenRootTypes['Stream'] | null; // Stream @@ -968,6 +982,10 @@ export interface NexusGenFieldTypes { // field return type label: NexusGenRootTypes['Label'] | null; // Label }; + DeleteLocationPayload: { + // field return type + location: NexusGenRootTypes['Location']; // Location! + }; DeleteSnapshotPayload: { // field return type snapshot: NexusGenRootTypes['Snapshot'] | null; // Snapshot @@ -1220,6 +1238,7 @@ export interface NexusGenFieldTypes { deleteBlueprint: NexusGenRootTypes['DeleteBlueprintPayload']; // DeleteBlueprintPayload! deleteDevice: NexusGenRootTypes['DeleteDevicePayload']; // DeleteDevicePayload! deleteLabel: NexusGenRootTypes['DeleteLabelPayload']; // DeleteLabelPayload! + deleteLocation: NexusGenRootTypes['DeleteLocationPayload']; // DeleteLocationPayload! deleteSnapshot: NexusGenRootTypes['DeleteSnapshotPayload'] | null; // DeleteSnapshotPayload deleteStream: NexusGenRootTypes['DeleteStreamPayload']; // DeleteStreamPayload! importCSV: NexusGenRootTypes['CSVImport'] | null; // CSVImport @@ -1234,6 +1253,7 @@ export interface NexusGenFieldTypes { updateDevice: NexusGenRootTypes['UpdateDevicePayload']; // UpdateDevicePayload! updateDiscoveredAt: NexusGenRootTypes['DeviceDiscoveryPayload'][]; // [DeviceDiscoveryPayload!]! updateGraphNodeCoordinates: NexusGenRootTypes['UpdateGraphNodeCoordinatesPayload']; // UpdateGraphNodeCoordinatesPayload! + updateLocation: NexusGenRootTypes['UpdateLocationPayload']; // UpdateLocationPayload! updateStream: NexusGenRootTypes['UpdateStreamPayload']; // UpdateStreamPayload! }; NetInterface: { @@ -1519,6 +1539,10 @@ export interface NexusGenFieldTypes { // field return type deviceNames: string[]; // [String!]! }; + UpdateLocationPayload: { + // field return type + location: NexusGenRootTypes['Location']; // Location! + }; UpdateStreamPayload: { // field return type stream: NexusGenRootTypes['Stream'] | null; // Stream @@ -1704,6 +1728,10 @@ export interface NexusGenFieldTypeNames { // field return type name label: 'Label'; }; + DeleteLocationPayload: { + // field return type name + location: 'Location'; + }; DeleteSnapshotPayload: { // field return type name snapshot: 'Snapshot'; @@ -1956,6 +1984,7 @@ export interface NexusGenFieldTypeNames { deleteBlueprint: 'DeleteBlueprintPayload'; deleteDevice: 'DeleteDevicePayload'; deleteLabel: 'DeleteLabelPayload'; + deleteLocation: 'DeleteLocationPayload'; deleteSnapshot: 'DeleteSnapshotPayload'; deleteStream: 'DeleteStreamPayload'; importCSV: 'CSVImport'; @@ -1970,6 +1999,7 @@ export interface NexusGenFieldTypeNames { updateDevice: 'UpdateDevicePayload'; updateDiscoveredAt: 'DeviceDiscoveryPayload'; updateGraphNodeCoordinates: 'UpdateGraphNodeCoordinatesPayload'; + updateLocation: 'UpdateLocationPayload'; updateStream: 'UpdateStreamPayload'; }; NetInterface: { @@ -2255,6 +2285,10 @@ export interface NexusGenFieldTypeNames { // field return type name deviceNames: 'String'; }; + UpdateLocationPayload: { + // field return type name + location: 'Location'; + }; UpdateStreamPayload: { // field return type name stream: 'Stream'; @@ -2386,6 +2420,10 @@ export interface NexusGenArgTypes { // args id: string; // String! }; + deleteLocation: { + // args + id: string; // String! + }; deleteSnapshot: { // args input: NexusGenInputs['DeleteSnapshotInput']; // DeleteSnapshotInput! @@ -2444,6 +2482,11 @@ export interface NexusGenArgTypes { // args input: NexusGenInputs['UpdateGraphNodeCoordinatesInput']; // UpdateGraphNodeCoordinatesInput! }; + updateLocation: { + // args + id: string; // String! + input: NexusGenInputs['UpdateLocationInput']; // UpdateLocationInput! + }; updateStream: { // args id: string; // String!