@defra/grants-reporting-publisher 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,7 +54,7 @@ jobs:
54
54
 
55
55
  - name: Create Release Pull Request or Release
56
56
  id: changesets
57
- uses: changesets/action@8488615a623b1b9c987934bb89eae8af6a946ac1 #v2.1.1
57
+ uses: changesets/action@ae32849d5ba541f9ae29e40e22a623bc13562f51 #v2.1.2
58
58
  with:
59
59
  publish-script: npm run github:release
60
60
  commit-message: 'Create new config version'
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @defra/grants-reporting-publisher
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 4a25189: Allow options to be sent with a status change messgae
8
+
9
+ ## 0.1.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 62e9659: Make some option fields optional
14
+
3
15
  ## 0.1.1
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-reporting-publisher",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "NPM module for publishing grants reporting events",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -48,14 +48,14 @@
48
48
  "@babel/preset-env": "8.0.2",
49
49
  "@changesets/changelog-github": "1.0.0",
50
50
  "@changesets/cli": "3.0.1",
51
- "@vitest/coverage-v8": "4.1.10",
51
+ "@vitest/coverage-v8": "5.0.0",
52
52
  "eslint": "10.7.0",
53
53
  "eslint-config-prettier": "10.1.1",
54
54
  "husky": "9.1.7",
55
- "lint-staged": "17.3.0",
55
+ "lint-staged": "17.4.1",
56
56
  "neostandard": "0.13.0",
57
57
  "prettier": "3.9.6",
58
- "vitest": "4.1.10",
58
+ "vitest": "5.0.0",
59
59
  "vitest-fetch-mock": "0.4.5"
60
60
  },
61
61
  "overrides": {
@@ -1,5 +1,5 @@
1
1
  import { publishReportingEvent } from "./publish.js";
2
- import { AGREEMENT_STATUS_CHANGED } from "./constants.js";
2
+ import { AGREEMENT_CREATED, AGREEMENT_STATUS_CHANGED } from "./constants.js";
3
3
 
4
4
  const mockSend = vi.hoisted(() =>
5
5
  vi.fn().mockResolvedValue({ MessageId: "msg-001" }),
@@ -162,6 +162,38 @@ describe("publishAuditEvent", () => {
162
162
  await publishReportingEvent(validEvent, baseConfig);
163
163
  expect(mockSend).toHaveBeenCalledTimes(1);
164
164
  });
165
+
166
+ test("publishes AGREEMENT_CREATED event with minimal options", async () => {
167
+ const agreementCreatedEvent = {
168
+ correlationId: "abc-123",
169
+ datetime: "2025-12-01T12:51:41.381Z",
170
+ version: "1.0.0",
171
+ application: "FCP001",
172
+ service: "grants",
173
+ eventData: {
174
+ eventType: AGREEMENT_CREATED,
175
+ agreementId: "WMP123456789",
176
+ agreementType: "WOODLAND",
177
+ agreementStatus: "ACCEPTED",
178
+ sbi: "200000001",
179
+ options: [
180
+ {
181
+ parcelReference: "SD8545-9935",
182
+ optionCode: "WMP1",
183
+ optionQuantity: 15.75,
184
+ optionValue: 1575.0,
185
+ },
186
+ ],
187
+ },
188
+ };
189
+
190
+ const result = await publishReportingEvent(
191
+ agreementCreatedEvent,
192
+ baseConfig,
193
+ );
194
+ expect(result).toEqual({ messageId: "msg-001" });
195
+ expect(mockSend).toHaveBeenCalledTimes(1);
196
+ });
165
197
  });
166
198
 
167
199
  describe("event validation", () => {
package/src/schema.js CHANGED
@@ -8,6 +8,19 @@ const accountId = Joi.alternatives()
8
8
  .try(Joi.string().max(MAX_50), Joi.number())
9
9
  .custom(String);
10
10
 
11
+ const optionsSchema = Joi.array().items(
12
+ Joi.object({
13
+ parcelReference: Joi.string().allow("").required(),
14
+ parcelSizeUnderAgreement: Joi.number().optional(),
15
+ optionCode: Joi.string().required(),
16
+ optionYear: Joi.number().integer().optional(),
17
+ optionStartDate: Joi.date().iso().optional(),
18
+ optionEndDate: Joi.date().iso().optional(),
19
+ optionQuantity: Joi.number().required(),
20
+ optionValue: Joi.number().required(),
21
+ }),
22
+ );
23
+
11
24
  export const eventSchema = Joi.object({
12
25
  correlationId: Joi.string().max(MAX_50).required(),
13
26
  datetime: Joi.date().iso().required(),
@@ -28,20 +41,7 @@ export const eventSchema = Joi.object({
28
41
  agreementEndDate: Joi.date().iso().optional(),
29
42
  agreementValue: Joi.number().optional(),
30
43
  sbi: accountId.required(),
31
- options: Joi.array()
32
- .items(
33
- Joi.object({
34
- parcelReference: Joi.string().allow("").required(),
35
- parcelSizeUnderAgreement: Joi.number().optional(),
36
- optionCode: Joi.string().required(),
37
- optionYear: Joi.number().integer().optional(),
38
- optionStartDate: Joi.date().iso().required(),
39
- optionEndDate: Joi.date().iso().required(),
40
- optionQuantity: Joi.number().required(),
41
- optionValue: Joi.number().required(),
42
- }),
43
- )
44
- .required(),
44
+ options: optionsSchema.required(),
45
45
  }),
46
46
  })
47
47
  .when(Joi.object({ eventType: AGREEMENT_STATUS_CHANGED }).unknown(), {
@@ -53,6 +53,7 @@ export const eventSchema = Joi.object({
53
53
  agreementEndDate: Joi.date().iso().optional(),
54
54
  agreementValue: Joi.number().optional(),
55
55
  userId: Joi.string().optional(),
56
+ options: optionsSchema.optional(),
56
57
  }),
57
58
  })
58
59
  .unknown()
@@ -187,6 +187,28 @@ describe("validateReportingEvent", () => {
187
187
  });
188
188
 
189
189
  test("accepts Agreement created event with minimal options", () => {
190
+ const result = validateReportingEvent({
191
+ ...validBase,
192
+ eventData: {
193
+ eventType: AGREEMENT_CREATED,
194
+ agreementId: "WMP123456789",
195
+ agreementType: "WOODLAND",
196
+ agreementStatus: "ACCEPTED",
197
+ sbi: "200000001",
198
+ options: [
199
+ {
200
+ parcelReference: "SD8545-9935",
201
+ optionCode: "WMP1",
202
+ optionQuantity: 15.75,
203
+ optionValue: 1575.0,
204
+ },
205
+ ],
206
+ },
207
+ });
208
+ expect(result.valid).toBe(true);
209
+ });
210
+
211
+ test("accepts Agreement created event with option containing optionStartDate only", () => {
190
212
  const result = validateReportingEvent({
191
213
  ...validBase,
192
214
  eventData: {
@@ -200,6 +222,28 @@ describe("validateReportingEvent", () => {
200
222
  parcelReference: "SD8545-9935",
201
223
  optionCode: "WMP1",
202
224
  optionStartDate: "2026-09-01",
225
+ optionQuantity: 15.75,
226
+ optionValue: 1575.0,
227
+ },
228
+ ],
229
+ },
230
+ });
231
+ expect(result.valid).toBe(true);
232
+ });
233
+
234
+ test("accepts Agreement created event with option containing optionEndDate only", () => {
235
+ const result = validateReportingEvent({
236
+ ...validBase,
237
+ eventData: {
238
+ eventType: AGREEMENT_CREATED,
239
+ agreementId: "WMP123456789",
240
+ agreementType: "WOODLAND",
241
+ agreementStatus: "ACCEPTED",
242
+ sbi: "200000001",
243
+ options: [
244
+ {
245
+ parcelReference: "SD8545-9935",
246
+ optionCode: "WMP1",
203
247
  optionEndDate: "2029-08-31",
204
248
  optionQuantity: 15.75,
205
249
  optionValue: 1575.0,
@@ -210,6 +254,58 @@ describe("validateReportingEvent", () => {
210
254
  expect(result.valid).toBe(true);
211
255
  });
212
256
 
257
+ test("rejects Agreement created event if optionStartDate is not a valid date", () => {
258
+ const result = validateReportingEvent({
259
+ ...validBase,
260
+ eventData: {
261
+ eventType: AGREEMENT_CREATED,
262
+ agreementId: "WMP123456789",
263
+ agreementType: "WOODLAND",
264
+ agreementStatus: "ACCEPTED",
265
+ sbi: "200000001",
266
+ options: [
267
+ {
268
+ parcelReference: "SD8545-9935",
269
+ optionCode: "WMP1",
270
+ optionStartDate: "invalid-date",
271
+ optionQuantity: 15.75,
272
+ optionValue: 1575.0,
273
+ },
274
+ ],
275
+ },
276
+ });
277
+ expect(result.valid).toBe(false);
278
+ expect(result.errors).toContain(
279
+ '"eventData.options[0].optionStartDate" must be in ISO 8601 date format',
280
+ );
281
+ });
282
+
283
+ test("rejects Agreement created event if optionEndDate is not a valid date", () => {
284
+ const result = validateReportingEvent({
285
+ ...validBase,
286
+ eventData: {
287
+ eventType: AGREEMENT_CREATED,
288
+ agreementId: "WMP123456789",
289
+ agreementType: "WOODLAND",
290
+ agreementStatus: "ACCEPTED",
291
+ sbi: "200000001",
292
+ options: [
293
+ {
294
+ parcelReference: "SD8545-9935",
295
+ optionCode: "WMP1",
296
+ optionEndDate: "invalid-date",
297
+ optionQuantity: 15.75,
298
+ optionValue: 1575.0,
299
+ },
300
+ ],
301
+ },
302
+ });
303
+ expect(result.valid).toBe(false);
304
+ expect(result.errors).toContain(
305
+ '"eventData.options[0].optionEndDate" must be in ISO 8601 date format',
306
+ );
307
+ });
308
+
213
309
  test("rejects Agreement created event if required fields are missing", () => {
214
310
  const result = validateReportingEvent({
215
311
  ...validBase,
@@ -298,6 +394,49 @@ describe("validateReportingEvent", () => {
298
394
  expect(result.valid).toBe(true);
299
395
  });
300
396
 
397
+ test("accepts a valid Agreement status changed event with options", () => {
398
+ const result = validateReportingEvent({
399
+ ...validBase,
400
+ eventData: {
401
+ eventType: AGREEMENT_STATUS_CHANGED,
402
+ agreementId: "WMP123456789",
403
+ agreementStatus: "ACCEPTED",
404
+ statusDate: "2026-09-02T10:00:00Z",
405
+ options: [
406
+ {
407
+ parcelReference: "SD8545-9935",
408
+ optionCode: "WMP1",
409
+ optionQuantity: 15.75,
410
+ optionValue: 1575.0,
411
+ },
412
+ ],
413
+ },
414
+ });
415
+ expect(result.valid).toBe(true);
416
+ });
417
+
418
+ test("rejects Agreement status changed event if options contain invalid data", () => {
419
+ const result = validateReportingEvent({
420
+ ...validBase,
421
+ eventData: {
422
+ eventType: AGREEMENT_STATUS_CHANGED,
423
+ agreementId: "WMP123456789",
424
+ agreementStatus: "ACCEPTED",
425
+ statusDate: "2026-09-02T10:00:00Z",
426
+ options: [
427
+ {
428
+ parcelReference: "SD8545-9935",
429
+ // missing mandatory option fields like optionCode
430
+ },
431
+ ],
432
+ },
433
+ });
434
+ expect(result.valid).toBe(false);
435
+ expect(result.errors).toContain(
436
+ '"eventData.options[0].optionCode" is required',
437
+ );
438
+ });
439
+
301
440
  test("rejects Agreement status changed event if required fields are missing", () => {
302
441
  const result = validateReportingEvent({
303
442
  ...validBase,