@@ -35,7 +35,6 @@ export const users = pgTable('users', {
3535 role : varchar ( 'role' , { length : 20 } ) . default ( 'USER' ) . notNull ( ) ,
3636 level : integer ( 'level' ) . default ( 1 ) . notNull ( ) ,
3737 experience : integer ( 'experience' ) . default ( 0 ) . notNull ( ) ,
38- organisationId : integer ( 'organisation_id' ) . references ( ( ) => organizations . id , { onDelete : 'set null' } ) ,
3938 recordStatus : varchar ( 'record_status' , { length : 20 } ) . default ( 'CREATED' ) . notNull ( ) ,
4039 createdAt : timestamp ( 'created_at' ) . defaultNow ( ) ,
4140 updatedAt : timestamp ( 'updated_at' ) . defaultNow ( ) ,
@@ -96,7 +95,11 @@ export const organizationOwners = pgTable('organization_owners', {
9695 . references ( ( ) => organizations . id )
9796 . notNull ( ) ,
9897 userId : integer ( 'user_id' ) . references ( ( ) => users . id ) . notNull ( ) ,
99- } ) ;
98+ } , ( table ) => ( {
99+ uniqueOrganizationUser : unique ( ) . on ( table . organizationId , table . userId ) ,
100+ uniqueUser : unique ( ) . on ( table . userId ) , // Один пользователь может иметь только одну организацию
101+ uniqueOrganization : unique ( ) . on ( table . organizationId ) , // Одна организация может быть указана только один раз
102+ } ) ) ;
100103
101104// Связующая таблица: виды помощи организаций
102105export const organizationHelpTypes = pgTable ( 'organization_help_types' , {
@@ -157,6 +160,7 @@ export const quests = pgTable('quests', {
157160 description ?: string ;
158161 status : string ;
159162 progress : number ;
163+ type : 'finance' | 'material' | 'contributers' ;
160164 requirement ?: {
161165 currentValue : number ;
162166 targetValue : number ;
@@ -222,6 +226,37 @@ export const userQuests = pgTable('user_quests', {
222226 uniqueUserQuest : unique ( ) . on ( table . userId , table . questId ) ,
223227} ) ) ;
224228
229+ // Связующая таблица: волонтёры этапов квестов (только finance и material)
230+ export const questStepVolunteers = pgTable ( 'quest_step_volunteers' , {
231+ id : serial ( 'id' ) . primaryKey ( ) ,
232+ questId : integer ( 'quest_id' )
233+ . references ( ( ) => quests . id )
234+ . notNull ( ) ,
235+ type : varchar ( 'type' , { length : 20 } ) . notNull ( ) , // 'finance' | 'material' | 'contributers'
236+ contributeValue : integer ( 'contribute_value' ) . notNull ( ) . default ( 0 ) ,
237+ userId : integer ( 'user_id' )
238+ . references ( ( ) => users . id )
239+ . notNull ( ) ,
240+ isInkognito : boolean ( 'is_inkognito' ) . default ( false ) . notNull ( ) ,
241+ recordStatus : varchar ( 'record_status' , { length : 20 } ) . default ( 'CREATED' ) . notNull ( ) ,
242+ createdAt : timestamp ( 'created_at' ) . defaultNow ( ) ,
243+ updatedAt : timestamp ( 'updated_at' ) . defaultNow ( ) ,
244+ } ) ;
245+
246+ // Связующая таблица: contributers квестов
247+ export const questContributers = pgTable ( 'quest_contributers' , {
248+ id : serial ( 'id' ) . primaryKey ( ) ,
249+ questId : integer ( 'quest_id' )
250+ . references ( ( ) => quests . id )
251+ . notNull ( ) ,
252+ userId : integer ( 'user_id' )
253+ . references ( ( ) => users . id )
254+ . notNull ( ) ,
255+ recordStatus : varchar ( 'record_status' , { length : 20 } ) . default ( 'CREATED' ) . notNull ( ) ,
256+ createdAt : timestamp ( 'created_at' ) . defaultNow ( ) ,
257+ updatedAt : timestamp ( 'updated_at' ) . defaultNow ( ) ,
258+ } ) ;
259+
225260// Relations
226261export const regionsRelations = relations ( regions , ( { many } ) => ( {
227262 cities : many ( cities ) ,
@@ -241,10 +276,8 @@ export const usersRelations = relations(users, ({ one, many }) => ({
241276 achievements : many ( userAchievements ) ,
242277 quests : many ( userQuests ) ,
243278 ownedQuests : many ( quests ) ,
244- organisation : one ( organizations , {
245- fields : [ users . organisationId ] ,
246- references : [ organizations . id ] ,
247- } ) ,
279+ stepVolunteers : many ( questStepVolunteers ) ,
280+ contributers : many ( questContributers ) ,
248281} ) ) ;
249282
250283export const helpTypesRelations = relations ( helpTypes , ( { many } ) => ( {
@@ -335,6 +368,8 @@ export const questsRelations = relations(quests, ({ one, many }) => ({
335368 userQuests : many ( userQuests ) ,
336369 categories : many ( questCategories ) ,
337370 updates : many ( questUpdates ) ,
371+ stepVolunteers : many ( questStepVolunteers ) ,
372+ contributers : many ( questContributers ) ,
338373} ) ) ;
339374
340375export const questCategoriesRelations = relations ( questCategories , ( { one } ) => ( {
@@ -373,4 +408,26 @@ export const userQuestsRelations = relations(userQuests, ({ one }) => ({
373408 } ) ,
374409} ) ) ;
375410
411+ export const questStepVolunteersRelations = relations ( questStepVolunteers , ( { one } ) => ( {
412+ user : one ( users , {
413+ fields : [ questStepVolunteers . userId ] ,
414+ references : [ users . id ] ,
415+ } ) ,
416+ quest : one ( quests , {
417+ fields : [ questStepVolunteers . questId ] ,
418+ references : [ quests . id ] ,
419+ } ) ,
420+ } ) ) ;
421+
422+ export const questContributersRelations = relations ( questContributers , ( { one } ) => ( {
423+ user : one ( users , {
424+ fields : [ questContributers . userId ] ,
425+ references : [ users . id ] ,
426+ } ) ,
427+ quest : one ( quests , {
428+ fields : [ questContributers . questId ] ,
429+ references : [ quests . id ] ,
430+ } ) ,
431+ } ) ) ;
432+
376433
0 commit comments