From b81056691f5d6f023c36c3c9a67475c320b4129b Mon Sep 17 00:00:00 2001 From: Fi1osof Date: Sun, 14 Mar 2021 09:30:15 +0300 Subject: [PATCH] Company::Schedules https://freecode.academy/tasks/ckm8pw0sog90r0730a8mrsbe5 --- server/nexus/generated/nexus.ts | 89 +++++++++++++++++++ server/nexus/generated/schema.graphql | 81 +++++++++++++++++ .../Query/definitions/Resource/Company.ts | 13 +-- .../Query/definitions/Resource/Resource.ts | 1 + .../types/Resource/Company/Schedule/index.ts | 51 +++++++++++ .../Resource/{Company.ts => Company/index.ts} | 39 +++++++- server/nexus/types/Resource/Resource.ts | 5 ++ server/nexus/types/index.ts | 1 + server/nexus/types/scalars/index.ts | 15 ++++ 9 files changed, 287 insertions(+), 8 deletions(-) create mode 100644 server/nexus/types/Resource/Company/Schedule/index.ts rename server/nexus/types/Resource/{Company.ts => Company/index.ts} (77%) create mode 100644 server/nexus/types/scalars/index.ts diff --git a/server/nexus/generated/nexus.ts b/server/nexus/generated/nexus.ts index d65808f..745eebc 100644 --- a/server/nexus/generated/nexus.ts +++ b/server/nexus/generated/nexus.ts @@ -645,6 +645,7 @@ export interface NexusGenScalars { Boolean: boolean ID: string DateTime: any + JSON: any } export interface NexusGenObjects { @@ -662,6 +663,7 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -692,6 +694,7 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! rating?: NexusGenRootTypes['VotesAvg'] | null // VotesAvg searchable: boolean // Boolean! @@ -725,6 +728,7 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -744,6 +748,7 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -764,11 +769,34 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! uri?: string | null // String } + Schedule: { + // root type + end?: NexusGenRootTypes['ScheduleData'] | null // ScheduleData + start: NexusGenRootTypes['ScheduleData'] // ScheduleData! + } + ScheduleData: { + // root type + day?: number | null // Int + hour?: number | null // Int + minute?: number | null // Int + month?: number | null // Int + second?: number | null // Int + weekDay?: number | null // Int + year?: number | null // Int + } + Schedules: { + // root type + Schedule?: Array | null // [Schedule] + ScheduleFamily?: Array | null // [Schedule] + ScheduleMen?: Array | null // [Schedule] + ScheduleWomen?: Array | null // [Schedule] + } Thread: { // root type id: number // Int! @@ -789,6 +817,7 @@ export interface NexusGenObjects { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties?: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -881,6 +910,7 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -901,6 +931,7 @@ export interface NexusGenFieldTypes { // field return type Comments: NexusGenRootTypes['Comment'][] // [Comment!]! CreatedBy: NexusGenRootTypes['User'] | null // User + Schedules: NexusGenRootTypes['Schedules'] | null // Schedules TemplateVarValues: | NexusGenRootTypes['bani684_site_tmplvar_contentvalues'][] | null // [bani684_site_tmplvar_contentvalues!] @@ -918,6 +949,7 @@ export interface NexusGenFieldTypes { longtitle: string // String! pagetitle: string // String! prices: string | null // String + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! rating: NexusGenRootTypes['VotesAvg'] | null // VotesAvg searchable: boolean // Boolean! @@ -971,6 +1003,7 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -991,6 +1024,7 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -1012,11 +1046,34 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! uri: string | null // String } + Schedule: { + // field return type + end: NexusGenRootTypes['ScheduleData'] | null // ScheduleData + start: NexusGenRootTypes['ScheduleData'] // ScheduleData! + } + ScheduleData: { + // field return type + day: number | null // Int + hour: number | null // Int + minute: number | null // Int + month: number | null // Int + second: number | null // Int + weekDay: number | null // Int + year: number | null // Int + } + Schedules: { + // field return type + Schedule: Array | null // [Schedule] + ScheduleFamily: Array | null // [Schedule] + ScheduleMen: Array | null // [Schedule] + ScheduleWomen: Array | null // [Schedule] + } Thread: { // field return type id: number // Int! @@ -1038,6 +1095,7 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -1111,6 +1169,7 @@ export interface NexusGenFieldTypes { id: number // Int! longtitle: string // String! pagetitle: string // String! + properties: NexusGenScalars['JSON'] | null // JSON published: boolean // Boolean! searchable: boolean // Boolean! template: number // Int! @@ -1133,6 +1192,7 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' @@ -1153,6 +1213,7 @@ export interface NexusGenFieldTypeNames { // field return type name Comments: 'Comment' CreatedBy: 'User' + Schedules: 'Schedules' TemplateVarValues: 'bani684_site_tmplvar_contentvalues' address: 'String' addressComments: 'String' @@ -1168,6 +1229,7 @@ export interface NexusGenFieldTypeNames { longtitle: 'String' pagetitle: 'String' prices: 'String' + properties: 'JSON' published: 'Boolean' rating: 'VotesAvg' searchable: 'Boolean' @@ -1219,6 +1281,7 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' @@ -1237,6 +1300,7 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' @@ -1256,11 +1320,34 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' uri: 'String' } + Schedule: { + // field return type name + end: 'ScheduleData' + start: 'ScheduleData' + } + ScheduleData: { + // field return type name + day: 'Int' + hour: 'Int' + minute: 'Int' + month: 'Int' + second: 'Int' + weekDay: 'Int' + year: 'Int' + } + Schedules: { + // field return type name + Schedule: 'Schedule' + ScheduleFamily: 'Schedule' + ScheduleMen: 'Schedule' + ScheduleWomen: 'Schedule' + } Thread: { // field return type name id: 'Int' @@ -1280,6 +1367,7 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' @@ -1351,6 +1439,7 @@ export interface NexusGenFieldTypeNames { id: 'Int' longtitle: 'String' pagetitle: 'String' + properties: 'JSON' published: 'Boolean' searchable: 'Boolean' template: 'Int' diff --git a/server/nexus/generated/schema.graphql b/server/nexus/generated/schema.graphql index af278e0..a068c3f 100644 --- a/server/nexus/generated/schema.graphql +++ b/server/nexus/generated/schema.graphql @@ -59,6 +59,10 @@ type City implements ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! @@ -108,6 +112,11 @@ type Company implements ResourceInterface { """ Comments: [Comment!]! CreatedBy: User + + """ + Расписание работы + """ + Schedules: Schedules TemplateVarValues: [bani684_site_tmplvar_contentvalues!] """ @@ -139,6 +148,10 @@ type Company implements ResourceInterface { Цены """ prices: String + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! """ @@ -231,6 +244,8 @@ input IntNullableFilter { notIn: [Int!] } +scalar JSON + input NestedBoolFilter { equals: Boolean not: NestedBoolFilter @@ -460,6 +475,10 @@ type Rating implements ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! @@ -481,6 +500,10 @@ type Resource implements ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! @@ -502,6 +525,10 @@ interface ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! @@ -527,12 +554,62 @@ type Review implements ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! uri: String } +""" +Время работы +""" +type Schedule { + end: ScheduleData + start: ScheduleData! +} + +""" +Данные времени работы +""" +type ScheduleData { + day: Int + hour: Int + minute: Int + month: Int + second: Int + weekDay: Int + year: Int +} + +""" +Все варианты расписания +""" +type Schedules { + """ + Общее расписание + """ + Schedule: [Schedule] + + """ + Расписание для семьи + """ + ScheduleFamily: [Schedule] + + """ + Расписание для мужчин + """ + ScheduleMen: [Schedule] + + """ + Расписание для женщин + """ + ScheduleWomen: [Schedule] +} + enum SortOrder { asc desc @@ -593,6 +670,10 @@ type Topic implements ResourceInterface { id: Int! longtitle: String! pagetitle: String! + properties: JSON + @deprecated( + reason: "Временно для получения данных расписания. Используйте вместо этого schedules" + ) published: Boolean! searchable: Boolean! template: Int! diff --git a/server/nexus/types/Query/definitions/Resource/Company.ts b/server/nexus/types/Query/definitions/Resource/Company.ts index bce1331..f77c039 100644 --- a/server/nexus/types/Query/definitions/Resource/Company.ts +++ b/server/nexus/types/Query/definitions/Resource/Company.ts @@ -124,6 +124,7 @@ export type CompaniesResult = { company_address: string | undefined company_workTime_id: number | undefined company_workTime: string | undefined + company_properties: string | undefined } function companiesQuery(this: PrismaContext['knex']) { @@ -240,6 +241,7 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = ( company_published: 'company.published', company_searchable: 'company.searchable', company_template: 'company.template', + company_properties: 'company.properties', }) .as('t') @@ -293,6 +295,7 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = ( company_address, company_workTime_id, company_workTime, + company_properties, } = n const TemplateVarValues: NexusGenObjects['Company']['TemplateVarValues'] = [] @@ -354,6 +357,7 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = ( description: company_description, longtitle: company_longtitle, // image: company_image, + properties: company_properties, TemplateVarValues, // type, // target_id, @@ -394,11 +398,10 @@ const companiesResolver: FieldResolver<'Query', 'companies'> = ( if (!bCoords) { return -1 } else if (!aCoords) { - - /** - * Если нет координат у предыдущей (не с чем сравнивать), - * остаемся на месте - */ + /** + * Если нет координат у предыдущей (не с чем сравнивать), + * остаемся на месте + */ return 0 } diff --git a/server/nexus/types/Query/definitions/Resource/Resource.ts b/server/nexus/types/Query/definitions/Resource/Resource.ts index 006d0a5..ab00c0f 100644 --- a/server/nexus/types/Query/definitions/Resource/Resource.ts +++ b/server/nexus/types/Query/definitions/Resource/Resource.ts @@ -79,6 +79,7 @@ export const Resource = (t: ObjectDefinitionBlock<'Query'>) => { content: true, editedby: true, editedon: true, + properties: true, TemplateVarValues: { select: { id: true, diff --git a/server/nexus/types/Resource/Company/Schedule/index.ts b/server/nexus/types/Resource/Company/Schedule/index.ts new file mode 100644 index 0000000..f8135e4 --- /dev/null +++ b/server/nexus/types/Resource/Company/Schedule/index.ts @@ -0,0 +1,51 @@ +import { objectType } from 'nexus' + +export const ScheduleData = objectType({ + name: 'ScheduleData', + description: 'Данные времени работы', + definition(t) { + t.int('year') + t.int('month') + t.int('day') + t.int('hour') + t.int('minute') + t.int('second') + t.int('weekDay') + }, +}) + +export const Schedule = objectType({ + name: 'Schedule', + description: 'Время работы', + definition(t) { + t.nonNull.field('start', { + type: 'ScheduleData', + }) + t.field('end', { + type: 'ScheduleData', + }) + }, +}) + +export const Schedules = objectType({ + name: 'Schedules', + description: 'Все варианты расписания', + definition(t) { + t.list.field('Schedule', { + type: 'Schedule', + description: 'Общее расписание', + }) + t.list.field('ScheduleMen', { + type: 'Schedule', + description: 'Расписание для мужчин', + }) + t.list.field('ScheduleWomen', { + type: 'Schedule', + description: 'Расписание для женщин', + }) + t.list.field('ScheduleFamily', { + type: 'Schedule', + description: 'Расписание для семьи', + }) + }, +}) diff --git a/server/nexus/types/Resource/Company.ts b/server/nexus/types/Resource/Company/index.ts similarity index 77% rename from server/nexus/types/Resource/Company.ts rename to server/nexus/types/Resource/Company/index.ts index 3eda689..1802f71 100644 --- a/server/nexus/types/Resource/Company.ts +++ b/server/nexus/types/Resource/Company/index.ts @@ -1,8 +1,11 @@ /* eslint-disable @typescript-eslint/camelcase */ import { FieldResolver, objectType } from 'nexus' -import { TemplateVarIDs } from '../../constants' -import { imageResolver } from '../Query/resolvers/image' -import { coords } from './definitions/coords' +import { NexusGenObjects } from 'server/nexus/generated/nexus' +import { TemplateVarIDs } from '../../../constants' +import { imageResolver } from '../../Query/resolvers/image' +import { coords } from '../definitions/coords' + +export * from './Schedule' const galleryResolver: FieldResolver<'Company', 'gallery'> = (parent) => { type File = { @@ -136,6 +139,36 @@ export const Company = objectType({ description: 'Средний рейтинг', }) + t.field('Schedules', { + type: 'Schedules', + description: 'Расписание работы', + resolve(parent) { + let Schedules: NexusGenObjects['Schedules'] | null = null + + if (parent.properties) { + try { + const { + schedule: Schedule, + schedule_men: ScheduleMen, + schedule_women: ScheduleWomen, + schedule_family: ScheduleFamily, + } = JSON.parse(parent.properties) + + Schedules = { + Schedule, + ScheduleMen, + ScheduleWomen, + ScheduleFamily, + } + } catch (error) { + console.error('Company::Schedules JSON parse error', error) + } + } + + return Schedules + }, + }) + t.nonNull.list.nonNull.field('gallery', { type: 'GalleryImage', resolve: galleryResolver, diff --git a/server/nexus/types/Resource/Resource.ts b/server/nexus/types/Resource/Resource.ts index 91eee2f..ebde17d 100644 --- a/server/nexus/types/Resource/Resource.ts +++ b/server/nexus/types/Resource/Resource.ts @@ -55,6 +55,11 @@ export const ResourceInterface = interfaceType({ t.nonNull.boolean('published') t.nonNull.int('createdby') t.nonNull.boolean('searchable') + t.field('properties', { + type: 'JSON', + deprecation: + 'Временно для получения данных расписания. Используйте вместо этого schedules', + }) t.list.nonNull.field('TemplateVarValues', { type: 'bani684_site_tmplvar_contentvalues', }) diff --git a/server/nexus/types/index.ts b/server/nexus/types/index.ts index d0316bd..b1c4992 100644 --- a/server/nexus/types/index.ts +++ b/server/nexus/types/index.ts @@ -7,4 +7,5 @@ export * from './TemplateVarValue' export * from './Coordinates' export * from './User' export * from './society' +export * from './scalars' export * from './TopicTag' diff --git a/server/nexus/types/scalars/index.ts b/server/nexus/types/scalars/index.ts new file mode 100644 index 0000000..3516f1c --- /dev/null +++ b/server/nexus/types/scalars/index.ts @@ -0,0 +1,15 @@ +import { GraphQLScalarType } from 'graphql' + +export const JSONScalar = new GraphQLScalarType({ + name: 'JSON', + // serialize: (data: unknown) => data, + serialize: (data: string | Record | Array) => { + try { + return data ? (typeof data === 'string' ? JSON.parse(data) : data) : null + } catch (error) { + console.error('JSONScalar parse error', error) + return null + } + }, + parseValue: (data: unknown) => data, +})